基于这段代码
输出结果为:
struct Foo
{
Foo()
{
cout << "default ctor" << endl;
}
Foo(std::initializer_list<Foo> ilist)
{
cout << "initializer list" << endl;
}
Foo(const Foo& copy)
{
cout << "copy ctor" << endl;
}
};
int main()
{
Foo a;
Foo b(a);
// This calls the copy constructor again!
//Shouldn't this call the initializer_list constructor?
Foo c{b};
_getch();
return 0;
}
输出结果为:
默认构造函数
复制构造函数
复制构造函数
在第三种情况下,我将b放入花括号初始化中,这应该调用initializer_list<>构造函数。
然而,复制构造函数却起到了主导作用。
有人能告诉我为什么会这样吗?