简体   繁体   English

C ++ 11 Curly Braces

[英]C++ 11 Curly Braces

I haven't used C++ for a good few years, and have just come across this: 我好几年没用过C ++了,刚刚遇到过这个问题:

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? 我假设它与函数指针有关,但这似乎是可选的?

std::vector has a constructor that takes an std::initializer_list . std::vector有一个构造函数,它接受一个std::initializer_list

An initializer_list can be expressed with curly braces. initializer_list可以用花括号表示。

So this code creates a vector with one default_device in it and passes it to the build member function. 因此,此代码创建一个带有一个default_device的向量,并将其传递给build成员函数。

In: 在:

program.build({ default_device })

you are automagically instantiating a temporary VECTOR_CLASS<Device> object. 您将自动实例化临时VECTOR_CLASS<Device>对象。 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::initializer_list构造函数

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM