<分区>
考虑代码:
#include <iostream>
struct Foo
{
Foo(int){}
operator bool() const
{
return true;
}
};
int main()
{
if(Foo foo{42})
{
std::cout << "ok\n";
}
}
它在 gcc5 下编译良好。但是,如果我将行 if(Foo foo{42})
替换为
if(Foo foo(42))
我得到一个编译时错误:
error: expected primary-expression before 'foo'
这是怎么回事?没有令人烦恼的解析 imo,那么为什么使用大括号有效?