Why class { int i; }; is not fully standard-conformant?

前端 未结 5 2115
深忆病人
深忆病人 2020-12-14 00:39

This is a follow-up question.

In the previous question, @JohannesSchaub-litb said that the following code is not fully standard-conformant:

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-14 00:49

    The error message from GCC explains it quite succinctly:

    $ cat > a.cc
    class { int i; };
    $ g++ -Wall -std=c++98 a.cc
    a.cc:1: error: abstract declarator ‘’ used as declaration
    

    class { int i; } is an abstract-declarator (Standard, §8) but not a valid declaration (§7). That's the rule that @JohannesSchaub-litb referenced: for a valid declaration, you need something to be declared, e.g. a class name or variable name.

提交回复
热议问题