sizeof in static const member initialization

前端 未结 4 1183
攒了一身酷
攒了一身酷 2021-01-23 14:00

I have such code:

class A
{
public:

    unsigned long a;
    static const unsigned long b = sizeof(a); // \"error C2327: \'A::a\' : is not a type name, static,          


        
4条回答
  •  我在风中等你
    2021-01-23 14:27

    Your MSVS versions are quite old, so based on that, and assuming they default to C++03, they are correct to reject your code. I'll quote n1905, which for our purposes is pretty close to the C++03 standard.

    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).

提交回复
热议问题