rhughes
rhughes

Reputation: 9583

C++ 11 Curly Braces

I haven't used C++ for a good few years, and have just come across this:

program.build({ default_device })

The definition is:

cl_int build(
    const VECTOR_CLASS<Device>& devices,
    const char* options = NULL,
    void (CL_CALLBACK * notifyFptr)(cl_program, void *) = NULL,
    void* data = NULL) const

What are the curly braces there for? I have never seen them used in a function call like this before. I assume it has something to do with the function pointer, but that seems optional?

Upvotes: 25

Views: 5547

Answers (3)

Wojtek Surowka
Wojtek Surowka

Reputation: 20993

These are initializer lists, se e.g. http://www.cplusplus.com/reference/initializer_list/initializer_list/

Upvotes: 3

typ1232
typ1232

Reputation: 5587

std::vector has a constructor that takes an std::initializer_list.

An initializer_list can be expressed with curly braces.

So this code creates a vector with one default_device in it and passes it to the build member function.

Upvotes: 29

Shoe
Shoe

Reputation: 76240

In:

program.build({ default_device })

you are automagically instantiating a temporary VECTOR_CLASS<Device> object. It is equivalent to:

program.build(VECTOR_CLASS<Device>{ default_device })

which is equivalent to:

program.build(std::vector<Device>{ default_device })

which will call the std::initializer_list constructor:

std::vector::vector(std::initializer_list<T> init, 
    const Allocator& alloc = Allocator());

Upvotes: 14

Related Questions