其他分享
首页 > 其他分享> > c – 为什么trailing-return-type不是类的完整类上下文?

c – 为什么trailing-return-type不是类的完整类上下文?

作者:互联网

请注意,[class.mem]p6中未提及尾随返回类型

A complete-class context of a class is a

(6.1) function body,
(6.2) default argument,
(6.3) noexcept-specifier ([except.spec]),
(6.4) contract condition, or
(6.5) default member initializer

within the member-specification of the class. [ Note: A complete-class
context of a nested class is also a complete-class context of any
enclosing class, if the nested class is defined within the
member-specification of the enclosing class. — end note ]

[expr.prim.this]p2还有一个关于此的说明:

If a declaration declares a member function or member function
template of a class X, the expression this is a prvalue of type
“pointer to cv-qualifier-seq X” between the optional cv-qualifier-seq
and the end of the function-definition, member-declarator, or
declarator. It shall not appear before the optional cv-qualifier-seq
and it shall not appear within the declaration of a static member
function (although its type and value category are defined within a
static member function as they are within a non-static member
function). [ Note: This is because declaration matching does not occur
until the complete declarator is known. — end note ] [ Note: In a
trailing-return-type, the class being defined is not required to be
complete for purposes of class member access. Class members declared
later are not visible.
[ Example: …

解决方法:

因为你不想要它.

 struct Test {
     auto foo() -> decltype(bar());
     auto bar() -> int;

     auto baz() -> decltype(qux());
     auto qux() -> decltype(baz());
 }; 

现在你需要各种规则来解释上面哪些是允许的,哪些不是.

那么为什么标准将noexcept-specifier放在完整的类上下文中呢?它不会在这样的代码中允许基本相同的东西:

struct Test { 
    void foo() noexcept(noexcept(bar())); 
    void bar() noexcept(noexcept(foo())); 
};

似乎标准没有很好地解决这个问题,编制者对此的处理也不同. Clang抱怨上面的代码,但吃了这个:

struct Test { 
    void foo() noexcept(Test::b);
    static const bool b = true;
};

GCC也抱怨第二个代码,但接受代码与成员声明交换.似乎它根本不将noexcept说明符视为完整类上下文.

标签:c,language-lawyer
来源: https://codeday.me/bug/20191007/1869582.html