其他分享
首页 > 其他分享> > c-静态const成员初始化中的sizeof

c-静态const成员初始化中的sizeof

作者:互联网

我有这样的代码:

class A
{
public:

    unsigned long a;
    static const unsigned long b = sizeof(a); // "error C2327: 'A::a' : is not a type name, static, or enumerator" in VC++
};

我在VC中出现编译器错误,在IAR中没有错误.
哪个编译器是正确的,C Standart对此有何评价?

解决方法:

您的MSVS版本很旧,因此基于此版本,并假设它们默认为C 03,则可以拒绝您的代码.我将引用n1905,对于我们来说,它非常接近C 03标准.

9.4 [class.static] (emphasis mine)

If an unqualified-id (5.1) is used in the definition of a static
member following the member’s declarator-id, and name lookup (3.4.1)
finds that the unqualified-id refers to a static member, enumerator,
or nested type of the member’s class (or of a base class of the
member’s class), the unqualified-id is transformed into a qualified-id
expression in which the nested-name-specifier names the class scope
from which the member is referenced. The definition of a static member
shall not use directly the names of the non-static members of its
class or of a base class of its class (including as operands of the
sizeof operator). The definition of a static member may only refer to
these members to form pointer to members (5.3.1) or with the class
member access syntax (5.2.5).

标签:member-variables,c,sizeof,visual-studio-2013,visual-studio-2005
来源: https://codeday.me/bug/20191013/1905621.html