Is it legal to define an unnamed struct?Why does C++ disallow anonymous structs?When should you use a class vs a struct in C++?Why isn't sizeof for a struct equal to the sum of sizeof of each member?Unnamed/anonymous namespaces vs. static functionsDifference between 'struct' and 'typedef struct' in C++?Rules for Anonymous AggregatesSunCC 5.12 through 5.14 and “Types cannot be declared in anonymous union”is it legal to declare inline but not actually inline a function in C++g++ and clang++ different behaviour with non-type argument in struct/class specializationCan an unnamed struct be made static?A compile-type template predicate compiles with Clang, but not with GCC or MSVC
Company stopped paying my salary. What are my options?
Hexagonal Grid Filling
How likely are Coriolis-effect-based quirks to develop in starship crew members?
Can you turn a recording upside-down?
Probability of taking balls without replacement from a bag question
Why is there a cap on 401k contributions?
Can I bring back Planetary Romance as a genre?
How does weapons training transfer to empty hand?
What's the difference between "ricochet" and "bounce"?
Lorentz invariance of Maxwell's equations in matter
My perfect evil overlord plan... or is it?
What's the "magic similar to the Knock spell" referenced in the Dungeon of the Mad Mage adventure?
Was the Highlands Ranch shooting the 115th mass shooting in the US in 2019
Two (probably) equal real numbers which are not proved to be equal?
Program for finding longest run of zeros from a list of 100 random integers which are either 0 or 1
Are on’yomi words loanwords?
What dice to use in a game that revolves around triangles?
Was Mohammed the most popular first name for boys born in Berlin in 2018?
Is it a good idea to copy a trader when investing?
Why is it wrong to *implement* myself a known, published, widely believed to be secure crypto algorithm?
how to find out if there's files in a folder and exit accordingly (in KSH)
Why did they wait for Quill to arrive?
Equation of the line that lies tangent to both circles
Why is valarray so slow on VS2015?
Is it legal to define an unnamed struct?
Why does C++ disallow anonymous structs?When should you use a class vs a struct in C++?Why isn't sizeof for a struct equal to the sum of sizeof of each member?Unnamed/anonymous namespaces vs. static functionsDifference between 'struct' and 'typedef struct' in C++?Rules for Anonymous AggregatesSunCC 5.12 through 5.14 and “Types cannot be declared in anonymous union”is it legal to declare inline but not actually inline a function in C++g++ and clang++ different behaviour with non-type argument in struct/class specializationCan an unnamed struct be made static?A compile-type template predicate compiles with Clang, but not with GCC or MSVC
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Is the following code legal?:
struct
int x;
;
This code simply defines an unnamed structure. I do not intend 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
add a comment |
Is the following code legal?:
struct
int x;
;
This code simply defines an unnamed structure. I do not intend 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
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
Apr 30 at 7:54
cplusplus.com/forum/general/138733
– Mayur
Apr 30 at 7:55
stackoverflow.com/questions/2253878/…
– Ville-Valtteri
Apr 30 at 8:03
Microsoft's compiler has (and the Windows headers use) an utterly pointless thing called an anonymous struct. That's why it accepts that code.
– Pete Becker
Apr 30 at 12:33
add a comment |
Is the following code legal?:
struct
int x;
;
This code simply defines an unnamed structure. I do not intend 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
Is the following code legal?:
struct
int x;
;
This code simply defines an unnamed structure. I do not intend 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
c++ c++11 unnamed-class
edited Apr 30 at 16:48
Boann
37.8k1291123
37.8k1291123
asked Apr 30 at 7:49
Igor GIgor G
1859
1859
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
Apr 30 at 7:54
cplusplus.com/forum/general/138733
– Mayur
Apr 30 at 7:55
stackoverflow.com/questions/2253878/…
– Ville-Valtteri
Apr 30 at 8:03
Microsoft's compiler has (and the Windows headers use) an utterly pointless thing called an anonymous struct. That's why it accepts that code.
– Pete Becker
Apr 30 at 12:33
add a comment |
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
Apr 30 at 7:54
cplusplus.com/forum/general/138733
– Mayur
Apr 30 at 7:55
stackoverflow.com/questions/2253878/…
– Ville-Valtteri
Apr 30 at 8:03
Microsoft's compiler has (and the Windows headers use) an utterly pointless thing called an anonymous struct. That's why it accepts that code.
– Pete Becker
Apr 30 at 12:33
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
Apr 30 at 7:54
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
Apr 30 at 7:54
cplusplus.com/forum/general/138733
– Mayur
Apr 30 at 7:55
cplusplus.com/forum/general/138733
– Mayur
Apr 30 at 7:55
stackoverflow.com/questions/2253878/…
– Ville-Valtteri
Apr 30 at 8:03
stackoverflow.com/questions/2253878/…
– Ville-Valtteri
Apr 30 at 8:03
Microsoft's compiler has (and the Windows headers use) an utterly pointless thing called an anonymous struct. That's why it accepts that code.
– Pete Becker
Apr 30 at 12:33
Microsoft's compiler has (and the Windows headers use) an utterly pointless thing called an anonymous struct. That's why it accepts that code.
– Pete Becker
Apr 30 at 12:33
add a comment |
1 Answer
1
active
oldest
votes
No, it is not allowed. GCC and Clang are right.
Per [dcl.dcl]/3 (7 Declarations) in N3337 (C++11 final draft), 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).)
The question is tagged c++11, and the wording isn't there for N3337
– StoryTeller
Apr 30 at 8:05
2
@StoryTeller In N3337, it is [dcl.dcl]/3
– Ville-Valtteri
Apr 30 at 8:07
1
Doesn'tstruct int n; ;
introduce the name::<anonymous struct>::n
?
– YSC
Apr 30 at 8:10
4
@YSC - That's not a name.
– StoryTeller
Apr 30 at 8:14
3
@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
Apr 30 at 8:19
|
show 6 more comments
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
No, it is not allowed. GCC and Clang are right.
Per [dcl.dcl]/3 (7 Declarations) in N3337 (C++11 final draft), 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).)
The question is tagged c++11, and the wording isn't there for N3337
– StoryTeller
Apr 30 at 8:05
2
@StoryTeller In N3337, it is [dcl.dcl]/3
– Ville-Valtteri
Apr 30 at 8:07
1
Doesn'tstruct int n; ;
introduce the name::<anonymous struct>::n
?
– YSC
Apr 30 at 8:10
4
@YSC - That's not a name.
– StoryTeller
Apr 30 at 8:14
3
@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
Apr 30 at 8:19
|
show 6 more comments
No, it is not allowed. GCC and Clang are right.
Per [dcl.dcl]/3 (7 Declarations) in N3337 (C++11 final draft), 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).)
The question is tagged c++11, and the wording isn't there for N3337
– StoryTeller
Apr 30 at 8:05
2
@StoryTeller In N3337, it is [dcl.dcl]/3
– Ville-Valtteri
Apr 30 at 8:07
1
Doesn'tstruct int n; ;
introduce the name::<anonymous struct>::n
?
– YSC
Apr 30 at 8:10
4
@YSC - That's not a name.
– StoryTeller
Apr 30 at 8:14
3
@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
Apr 30 at 8:19
|
show 6 more comments
No, it is not allowed. GCC and Clang are right.
Per [dcl.dcl]/3 (7 Declarations) in N3337 (C++11 final draft), 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).)
No, it is not allowed. GCC and Clang are right.
Per [dcl.dcl]/3 (7 Declarations) in N3337 (C++11 final draft), 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).)
edited May 1 at 2:02
answered Apr 30 at 8:01
L. F.L. F.
2,125629
2,125629
The question is tagged c++11, and the wording isn't there for N3337
– StoryTeller
Apr 30 at 8:05
2
@StoryTeller In N3337, it is [dcl.dcl]/3
– Ville-Valtteri
Apr 30 at 8:07
1
Doesn'tstruct int n; ;
introduce the name::<anonymous struct>::n
?
– YSC
Apr 30 at 8:10
4
@YSC - That's not a name.
– StoryTeller
Apr 30 at 8:14
3
@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
Apr 30 at 8:19
|
show 6 more comments
The question is tagged c++11, and the wording isn't there for N3337
– StoryTeller
Apr 30 at 8:05
2
@StoryTeller In N3337, it is [dcl.dcl]/3
– Ville-Valtteri
Apr 30 at 8:07
1
Doesn'tstruct int n; ;
introduce the name::<anonymous struct>::n
?
– YSC
Apr 30 at 8:10
4
@YSC - That's not a name.
– StoryTeller
Apr 30 at 8:14
3
@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
Apr 30 at 8:19
The question is tagged c++11, and the wording isn't there for N3337
– StoryTeller
Apr 30 at 8:05
The question is tagged c++11, and the wording isn't there for N3337
– StoryTeller
Apr 30 at 8:05
2
2
@StoryTeller In N3337, it is [dcl.dcl]/3
– Ville-Valtteri
Apr 30 at 8:07
@StoryTeller In N3337, it is [dcl.dcl]/3
– Ville-Valtteri
Apr 30 at 8:07
1
1
Doesn't
struct int n; ;
introduce the name ::<anonymous struct>::n
?– YSC
Apr 30 at 8:10
Doesn't
struct int n; ;
introduce the name ::<anonymous struct>::n
?– YSC
Apr 30 at 8:10
4
4
@YSC - That's not a name.
– StoryTeller
Apr 30 at 8:14
@YSC - That's not a name.
– StoryTeller
Apr 30 at 8:14
3
3
@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
Apr 30 at 8:19
@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
Apr 30 at 8:19
|
show 6 more comments
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
Apr 30 at 7:54
cplusplus.com/forum/general/138733
– Mayur
Apr 30 at 7:55
stackoverflow.com/questions/2253878/…
– Ville-Valtteri
Apr 30 at 8:03
Microsoft's compiler has (and the Windows headers use) an utterly pointless thing called an anonymous struct. That's why it accepts that code.
– Pete Becker
Apr 30 at 12:33