c++ - 在指向成员的指针声明中使用 'decltype' 是否有效?

标签 c++ language-lawyer iso decltype pointer-to-member

想象一下,出于某种奇怪的原因,我写下了这个:

int main()
{
    struct S
    {        
        int i;
    } var;


    int decltype(var)::* pint = &decltype(var)::i;
}

GCC尽管 Clang 似乎编译得很好失败并出现一些不确定的语法相关错误消息。

那么神圣的 ISO 文件对此有何评论 - 这是否有效?

最佳答案

这实际上是一个known bug在 Clang 中。

代码有效。

N4140 [dcl.mptr]/1:

In a declaration T D where D has the form

nested-name-specifier * attribute-specifier-seqopt cv-qualifier-seqopt D1

and the nested-name-specifier denotes a class, and the type of the identifier in the declaration T D1 is “derived-declarator-type-list T”, then the type of the identifier of D is “derived-declarator-type-list cv-qualifier-seq pointer to member of class nested-name-specifier of type T”. The optional attribute-specifier-seq (7.6.1) appertains to the pointer-to-member.

在这个定义中,我们对nested-name-specifier 感兴趣,它在 [expr.prim.general]/8 中定义为(强调我的):

nested-name-specifier:

:: type-name ::
namespace-name ::
decltype-specifier ::
nested-name-specifier identifier ::
nested-name-specifier templateopt simple-template-id ::

关于c++ - 在指向成员的指针声明中使用 'decltype' 是否有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31206462/

相关文章:

c++ - 在 std::map<..., boost::any> 中初始化子图

c++ - Critical Section 对象如何适用于多种方法

c++ - Valgrind 在空析构函数上给出 "Invalid write of size 8"

c++ - 静态模板化 constexpr 嵌套类成员

c++ - 在同一表达式内的表达式中使用副作用结果是否安全?

c++ - 面部检测不起作用

javascript - 带有 *this* 的简写属性名称

java - iso 的 pig UDF 到 yyyy-mm-dd hh :mm:ss. 000

iphone - iOS 应用程序因区域设置更改而崩溃

vmware-server - 使用 iso 创建 VMware vmdk