Is it legal to define an unnamed struct?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







11















Is the following code legal:



struct
{
int x;
};


This code simply defines an unnamed structure. I do not intent to create objects of this type, nor do I need this structure in any other way. It simply appears in the source as a side effect of some complex macro expansion.



Useless though it is, I see no problem with it. Just another piece of code that can be compiled and then optimized out completely.



However, in the real world the outcome is quite different from my expectations:



gcc 8.3 reports an error:




error: abstract declarator '<unnamed struct>' used as declaration




clang 8.0.0 reports an error too:




error: anonymous structs and classes must be class members

warning: declaration does not declare anything [-Wmissing-declarations]




Only MSVC 2017 sees no problem with such source.



So, the question is: who's right? Is there a relevant quote from the Standard that explicitly forbids such declarations?



Edit:

The project uses C++11. But the error messages are the same for C++98, C++11 and C++17.










share|improve this question

























  • According to stackoverflow.com/a/12785369/969365 C++ does not allow anonymous structs (but it says that C11 does, and that future revisions to C++ might add support). Note that there are differences between anonoymous structs and unnamed structs.

    – simon
    44 mins ago













  • cplusplus.com/forum/general/138733

    – Mayur
    42 mins ago











  • stackoverflow.com/questions/2253878/…

    – Ville-Valtteri
    35 mins ago


















11















Is the following code legal:



struct
{
int x;
};


This code simply defines an unnamed structure. I do not intent to create objects of this type, nor do I need this structure in any other way. It simply appears in the source as a side effect of some complex macro expansion.



Useless though it is, I see no problem with it. Just another piece of code that can be compiled and then optimized out completely.



However, in the real world the outcome is quite different from my expectations:



gcc 8.3 reports an error:




error: abstract declarator '<unnamed struct>' used as declaration




clang 8.0.0 reports an error too:




error: anonymous structs and classes must be class members

warning: declaration does not declare anything [-Wmissing-declarations]




Only MSVC 2017 sees no problem with such source.



So, the question is: who's right? Is there a relevant quote from the Standard that explicitly forbids such declarations?



Edit:

The project uses C++11. But the error messages are the same for C++98, C++11 and C++17.










share|improve this question

























  • According to stackoverflow.com/a/12785369/969365 C++ does not allow anonymous structs (but it says that C11 does, and that future revisions to C++ might add support). Note that there are differences between anonoymous structs and unnamed structs.

    – simon
    44 mins ago













  • cplusplus.com/forum/general/138733

    – Mayur
    42 mins ago











  • stackoverflow.com/questions/2253878/…

    – Ville-Valtteri
    35 mins ago














11












11








11








Is the following code legal:



struct
{
int x;
};


This code simply defines an unnamed structure. I do not intent to create objects of this type, nor do I need this structure in any other way. It simply appears in the source as a side effect of some complex macro expansion.



Useless though it is, I see no problem with it. Just another piece of code that can be compiled and then optimized out completely.



However, in the real world the outcome is quite different from my expectations:



gcc 8.3 reports an error:




error: abstract declarator '<unnamed struct>' used as declaration




clang 8.0.0 reports an error too:




error: anonymous structs and classes must be class members

warning: declaration does not declare anything [-Wmissing-declarations]




Only MSVC 2017 sees no problem with such source.



So, the question is: who's right? Is there a relevant quote from the Standard that explicitly forbids such declarations?



Edit:

The project uses C++11. But the error messages are the same for C++98, C++11 and C++17.










share|improve this question
















Is the following code legal:



struct
{
int x;
};


This code simply defines an unnamed structure. I do not intent to create objects of this type, nor do I need this structure in any other way. It simply appears in the source as a side effect of some complex macro expansion.



Useless though it is, I see no problem with it. Just another piece of code that can be compiled and then optimized out completely.



However, in the real world the outcome is quite different from my expectations:



gcc 8.3 reports an error:




error: abstract declarator '<unnamed struct>' used as declaration




clang 8.0.0 reports an error too:




error: anonymous structs and classes must be class members

warning: declaration does not declare anything [-Wmissing-declarations]




Only MSVC 2017 sees no problem with such source.



So, the question is: who's right? Is there a relevant quote from the Standard that explicitly forbids such declarations?



Edit:

The project uses C++11. But the error messages are the same for C++98, C++11 and C++17.







c++ c++11 unnamed-class






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 22 mins ago









StoryTeller

107k15225288




107k15225288










asked 49 mins ago









Igor GIgor G

1558




1558













  • According to stackoverflow.com/a/12785369/969365 C++ does not allow anonymous structs (but it says that C11 does, and that future revisions to C++ might add support). Note that there are differences between anonoymous structs and unnamed structs.

    – simon
    44 mins ago













  • cplusplus.com/forum/general/138733

    – Mayur
    42 mins ago











  • stackoverflow.com/questions/2253878/…

    – Ville-Valtteri
    35 mins ago



















  • According to stackoverflow.com/a/12785369/969365 C++ does not allow anonymous structs (but it says that C11 does, and that future revisions to C++ might add support). Note that there are differences between anonoymous structs and unnamed structs.

    – simon
    44 mins ago













  • cplusplus.com/forum/general/138733

    – Mayur
    42 mins ago











  • stackoverflow.com/questions/2253878/…

    – Ville-Valtteri
    35 mins ago

















According to stackoverflow.com/a/12785369/969365 C++ does not allow anonymous structs (but it says that C11 does, and that future revisions to C++ might add support). Note that there are differences between anonoymous structs and unnamed structs.

– simon
44 mins ago







According to stackoverflow.com/a/12785369/969365 C++ does not allow anonymous structs (but it says that C11 does, and that future revisions to C++ might add support). Note that there are differences between anonoymous structs and unnamed structs.

– simon
44 mins ago















cplusplus.com/forum/general/138733

– Mayur
42 mins ago





cplusplus.com/forum/general/138733

– Mayur
42 mins ago













stackoverflow.com/questions/2253878/…

– Ville-Valtteri
35 mins ago





stackoverflow.com/questions/2253878/…

– Ville-Valtteri
35 mins ago












1 Answer
1






active

oldest

votes


















9














No, it is not allowed. GCC and Clang are right.



Per N3337 (C++11 final draft) [dcl.dcl]/3 (7 Declarations), a class declaration must introduce at one name to the program. For example, the following are invalid:



enum { };
typedef class { };


(Note: this isn’t unique to C++11. In N4140 (C++14 final draft) it is [dcl.dcl]/5 (7 Declarations). In N4659 (C++17 final draft) it is [dcl.dcl]/5 (10 Declarations).)






share|improve this answer


























  • The question is tagged c++11, and the wording isn't there for N3337

    – StoryTeller
    33 mins ago






  • 2





    @StoryTeller In N3337, it is [dcl.dcl]/3

    – Ville-Valtteri
    31 mins ago






  • 1





    Doesn't struct { int n; }; introduce the name ::<anonymous struct>::n?

    – YSC
    27 mins ago








  • 1





    @YSC - That's not a name.

    – StoryTeller
    24 mins ago






  • 1





    @YSC - Yes, and in the scope of a class, a name may refer to a member. The fact you need to fully qualify it, means it doesn't introduce that name at all. This is about declarative regions and scopes.

    – StoryTeller
    19 mins ago














Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55916208%2fis-it-legal-to-define-an-unnamed-struct%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









9














No, it is not allowed. GCC and Clang are right.



Per N3337 (C++11 final draft) [dcl.dcl]/3 (7 Declarations), a class declaration must introduce at one name to the program. For example, the following are invalid:



enum { };
typedef class { };


(Note: this isn’t unique to C++11. In N4140 (C++14 final draft) it is [dcl.dcl]/5 (7 Declarations). In N4659 (C++17 final draft) it is [dcl.dcl]/5 (10 Declarations).)






share|improve this answer


























  • The question is tagged c++11, and the wording isn't there for N3337

    – StoryTeller
    33 mins ago






  • 2





    @StoryTeller In N3337, it is [dcl.dcl]/3

    – Ville-Valtteri
    31 mins ago






  • 1





    Doesn't struct { int n; }; introduce the name ::<anonymous struct>::n?

    – YSC
    27 mins ago








  • 1





    @YSC - That's not a name.

    – StoryTeller
    24 mins ago






  • 1





    @YSC - Yes, and in the scope of a class, a name may refer to a member. The fact you need to fully qualify it, means it doesn't introduce that name at all. This is about declarative regions and scopes.

    – StoryTeller
    19 mins ago


















9














No, it is not allowed. GCC and Clang are right.



Per N3337 (C++11 final draft) [dcl.dcl]/3 (7 Declarations), a class declaration must introduce at one name to the program. For example, the following are invalid:



enum { };
typedef class { };


(Note: this isn’t unique to C++11. In N4140 (C++14 final draft) it is [dcl.dcl]/5 (7 Declarations). In N4659 (C++17 final draft) it is [dcl.dcl]/5 (10 Declarations).)






share|improve this answer


























  • The question is tagged c++11, and the wording isn't there for N3337

    – StoryTeller
    33 mins ago






  • 2





    @StoryTeller In N3337, it is [dcl.dcl]/3

    – Ville-Valtteri
    31 mins ago






  • 1





    Doesn't struct { int n; }; introduce the name ::<anonymous struct>::n?

    – YSC
    27 mins ago








  • 1





    @YSC - That's not a name.

    – StoryTeller
    24 mins ago






  • 1





    @YSC - Yes, and in the scope of a class, a name may refer to a member. The fact you need to fully qualify it, means it doesn't introduce that name at all. This is about declarative regions and scopes.

    – StoryTeller
    19 mins ago
















9












9








9







No, it is not allowed. GCC and Clang are right.



Per N3337 (C++11 final draft) [dcl.dcl]/3 (7 Declarations), a class declaration must introduce at one name to the program. For example, the following are invalid:



enum { };
typedef class { };


(Note: this isn’t unique to C++11. In N4140 (C++14 final draft) it is [dcl.dcl]/5 (7 Declarations). In N4659 (C++17 final draft) it is [dcl.dcl]/5 (10 Declarations).)






share|improve this answer















No, it is not allowed. GCC and Clang are right.



Per N3337 (C++11 final draft) [dcl.dcl]/3 (7 Declarations), a class declaration must introduce at one name to the program. For example, the following are invalid:



enum { };
typedef class { };


(Note: this isn’t unique to C++11. In N4140 (C++14 final draft) it is [dcl.dcl]/5 (7 Declarations). In N4659 (C++17 final draft) it is [dcl.dcl]/5 (10 Declarations).)







share|improve this answer














share|improve this answer



share|improve this answer








edited 3 mins ago









StoryTeller

107k15225288




107k15225288










answered 37 mins ago









L. F.L. F.

1,687524




1,687524













  • The question is tagged c++11, and the wording isn't there for N3337

    – StoryTeller
    33 mins ago






  • 2





    @StoryTeller In N3337, it is [dcl.dcl]/3

    – Ville-Valtteri
    31 mins ago






  • 1





    Doesn't struct { int n; }; introduce the name ::<anonymous struct>::n?

    – YSC
    27 mins ago








  • 1





    @YSC - That's not a name.

    – StoryTeller
    24 mins ago






  • 1





    @YSC - Yes, and in the scope of a class, a name may refer to a member. The fact you need to fully qualify it, means it doesn't introduce that name at all. This is about declarative regions and scopes.

    – StoryTeller
    19 mins ago





















  • The question is tagged c++11, and the wording isn't there for N3337

    – StoryTeller
    33 mins ago






  • 2





    @StoryTeller In N3337, it is [dcl.dcl]/3

    – Ville-Valtteri
    31 mins ago






  • 1





    Doesn't struct { int n; }; introduce the name ::<anonymous struct>::n?

    – YSC
    27 mins ago








  • 1





    @YSC - That's not a name.

    – StoryTeller
    24 mins ago






  • 1





    @YSC - Yes, and in the scope of a class, a name may refer to a member. The fact you need to fully qualify it, means it doesn't introduce that name at all. This is about declarative regions and scopes.

    – StoryTeller
    19 mins ago



















The question is tagged c++11, and the wording isn't there for N3337

– StoryTeller
33 mins ago





The question is tagged c++11, and the wording isn't there for N3337

– StoryTeller
33 mins ago




2




2





@StoryTeller In N3337, it is [dcl.dcl]/3

– Ville-Valtteri
31 mins ago





@StoryTeller In N3337, it is [dcl.dcl]/3

– Ville-Valtteri
31 mins ago




1




1





Doesn't struct { int n; }; introduce the name ::<anonymous struct>::n?

– YSC
27 mins ago







Doesn't struct { int n; }; introduce the name ::<anonymous struct>::n?

– YSC
27 mins ago






1




1





@YSC - That's not a name.

– StoryTeller
24 mins ago





@YSC - That's not a name.

– StoryTeller
24 mins ago




1




1





@YSC - Yes, and in the scope of a class, a name may refer to a member. The fact you need to fully qualify it, means it doesn't introduce that name at all. This is about declarative regions and scopes.

– StoryTeller
19 mins ago







@YSC - Yes, and in the scope of a class, a name may refer to a member. The fact you need to fully qualify it, means it doesn't introduce that name at all. This is about declarative regions and scopes.

– StoryTeller
19 mins ago






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55916208%2fis-it-legal-to-define-an-unnamed-struct%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Alcázar de San Juan

Griza ansero

Heinkel He 51