gpt4 book ai didi

c++ - 根据我对 §3.4.1/8 的解释,这段代码应该可以编译。我错过了什么?

转载 作者:太空狗 更新时间:2023-10-29 23:32:44 25 4
gpt4 key购买 nike

根据来自 N4140 的 [basic.lookup.unqual]/8下面的片段应该编译。但它不在 clang 中, gcc 和 vs2013。

struct C {
void f(I) {}
using I = int;
};

[basic.lookup.unqual]/8(重点是我的):

For the members of a class X, a name used in a member function body, in a default argument, in an exception-specification, in the brace-or-equal-initializer of a non-static data member (9.2), or in the definition of a class member outside of the definition of X, following the member’s declarator-id31, shall be declared in one of the following ways:

  • before its use in the block in which it is used or in an enclosing block (6.3), or
  • shall be a member of class X or be a member of a base class of X (10.2), or
  • ...

31) That is, an unqualified name that occurs, for instance, in a type in the parameter-declaration-clause or in the exceptions-pecification.

最佳答案

您使用它的方式不符合[basic.lookup.unqual]/8 中的任何条件,它未被使用:

  • 在成员函数体中
  • 在默认参数中
  • 在异常规范中
  • 在非静态数据成员的大括号或相等初始化器中

而且它也不属于这种情况:

definition of a class member outside of the definition of X

以下示例显示了一些确实遵循这些规则的情况:

struct C {
int y = I() ; // brace-or-equal-initializer of non-static data member
void f(int x = I()) // default argument
noexcept(sizeof(I)<4) // exception-specification
{
I i = x ; // member function body
}
using I = int;
};

关于c++ - 根据我对 §3.4.1/8 的解释,这段代码应该可以编译。我错过了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31457627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com