这段代码如何允许调用 foo 而无需在施工现场命名 Vec 类型?这种语法是 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}
}
我确定这是一个重复的问题,但我不知道要搜索什么。
最佳答案
如 Piotr S. 所述,这是 copy list initialization . braced-init-list 用于初始化函数参数。在复制列表初始化中,只考虑非显式构造函数。这意味着如果 Vec::Vec(int, int)
是显式的,{0, 0}
会导致编译器错误并且 Vec{0, 0}
将是必需的。
关于c++ - foo({0, 0}) : Is this using initializer lists?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26997932/