【发布时间】:2019-06-27 12:06:09
【问题描述】:
考虑以下代码sn-p:
#include <iostream>
struct A {
A() {}
A(const A&) {}
};
struct B {
B(const A&) {}
};
void f(const A&) { std::cout << "A" << std::endl; }
void f(const B&) { std::cout << "B" << std::endl; }
int main() {
A a;
f( {a} ); // A
f( {{a}} ); // ambiguous
f( {{{a}}} ); // B
f({{{{a}}}}); // no matching function
}
为什么每个调用都制造相应的输出?大括号的数量如何影响统一初始化?大括号省略对这一切有何影响?
【问题讨论】:
-
完全被吸引了。但我认为你最好发布你的编译器消息。 :)
-
@Constructor cmets 在使用(至少)g++ 编译时给出错误消息,否则在执行期间写入的内容
-
可能要加标签
language-lawyer -
Nit:成员函数定义后的分号毫无意义,让外行想象所有右大括号都应该加一个。
-
@Rakete1111:null statements 比 null declarations 有更多用途(它们也不是语句)。
标签: c++ c++11 language-lawyer uniform-initialization list-initialization