在 §8.2[dcl.ambig.res]/2 中,我们有以下注释(重点是我的):
[ Note: A declaration can be explicitly disambiguated by a nonfunction-style cast, by an = to indicate initialization or by removing the redundant parentheses around the parameter name. —end note ]
不应该是inserting而不是上面的remove吗?
考虑以下示例:
#include <iostream>
struct S{ int i; S(int j) : i(j) {} };
float f = 1.0f;
S s(int(f)); // function declaration
int main()
{
std::cout << s.i << '\n';
}
代码无法编译,因为编译器将声明 S s(int(f));
视为函数声明。但是,如果我们确实在参数名称 f
周围插入括号,如 S s((int(f)));
,代码将编译并打印 1。
最佳答案
我必须同意 Simple 的评论,它告诉您参数名称周围的括号是多余的。 defect report 340: Unclear wording in disambiguation section 加强了这一点它作为非缺陷关闭并给出了以下示例:
struct Point
{
Point(int){}
};
struct Lattice
{
Lattice(Point, Point, int){}
};
int main(void)
{
int a, b;
Lattice latt(Point(a), Point(b), 3); /* Line X */
}
并说:
The declaration of latt declares a function with a return value of the type Lattice and taking three arguments. The type of the first two arguments is Point and each of these arguments is followed by a parameter name in redundant parentheses. The type of the third argument can not be determined, because it is a literal. This will result in a syntax error.
关于c++ - N4140 §8.2[dcl.ambig.res]/2 中的注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33301356/