简体   繁体   English

foo({0,0}):这是否使用初始化列表?

[英]foo({0, 0}): Is this using initializer lists?

How does this code allow calling foo without naming the Vec type at the construction site? 此代码如何允许在不指定施工现场Vec类型的情况下调用foo? Is this syntax a case of C++11 initializer lists? 这种语法是C ++ 11初始化程序列表的一种情况吗?

struct Vec {
    Vec(int x, int y) {
    }
};
void foo(Vec) {
}
int main() {
    foo({0, 0}); // normally I'd pass Vec(0, 0) or Vec{0, 0}
}

I'm sure this is a duplicate question but I don't know what to search for. 我确定这是一个重复的问题,但我不知道要搜索什么。

As stated by Piotr S., this is copy list initialization . 如Piotr S.所述,这是副本列表初始化 The braced-init-list is used to initialize the function parameter. braced-init-list用于初始化功能参数。 In copy-list-initialization, only non-explicit constructors are considered. 在复制列表初始化中,仅考虑非显式构造函数。 This means that if Vec::Vec(int, int) was explicit, {0, 0} would cause a compiler error and Vec{0, 0} would be required. 这意味着如果Vec::Vec(int, int)是显式的,则{0, 0}会导致编译器错误,并且需要Vec{0, 0}

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

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