c++ - N4140 §8.2[dcl.ambig.res]/2 中的注释

标签 c++ language-lawyer c++14 disambiguation

§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/

相关文章:

c++ - 仅在发布版本中出现 fatal error C1047

c++ - 具体类型的 unique_ptr

c++ - [expr.ref]/2 中的左值到右值转换

c - 只有一个线程使用 memory_order_seq_cst 有用吗?

c++ - 将实验性命名空间注入(inject) std

c++ - 让程序重新开始并获得新值?

c++ - 我需要非常快地计算斯特林的近似值

c++ - 返回对 InputIterator 内部状态的引用是否合法?

c++ - 返回类型自动扣除的好友函数模板无法访问私有(private)成员

c++ - 如何解决C++中的异步任务错误