10

Is there any difference in any aspect (syntactic limitation, performance, etc.) between the following two definitions?

using Foo = struct { int a, b, c; };

struct Foo { int a, b, c; };

(I'm asking because the first form is aesthetically more uniform when put among a lot of using declarations.)

EDIT: The post linked to in the comment doesn't exactly answer my question. I'm more concerned about how the above two definitions differ in terms of usage, whereas that post mainly answers how they are different in terms of, well, what they are, I think.

Zizheng Tai
  • 6,170
  • 28
  • 79

1 Answers1

13

Here are some differences I can think of:

  • (obvious) You can't declare any constructors, a destructor, or an assignment operator for an unnamed class.
  • You can't forward-declare an unnamed class, including as a friend of another class.
  • You can't mark an unnamed class final.
  • struct Foo can be declared in the same declarative region as a function or variable named Foo, although, obviously, you shouldn't do this. using Foo = ... does not allow you this freedom.
Brian Bi
  • 111,498
  • 10
  • 176
  • 312
  • 4
    Also: "(§ 9.4.2.4 [class.static.data]) Unnamed classes and classes contained directly or indirectly within unnamed classes shall not contain static data members." – AndyG Jul 18 '16 at 21:21
  • @AndyG Clang seems to [allow it](http://coliru.stacked-crooked.com/a/fd7e45aec6546ba30) but not gcc. – David G Jul 18 '16 at 21:31
  • 1
    Can you also not use `struct Foo` to refer to the class? – user253751 Jul 19 '16 at 05:37
  • @immibis when I tried it, I got an error that the using declaration was redeclaring `Foo` as a different kind of name... – Brian Bi Jul 19 '16 at 19:43
  • @Brian I mean `using Foo = ...; struct Foo instanceOfFoo;`. I presume that doesn't work but I haven't tried it. – user253751 Jul 20 '16 at 06:31
  • @immibis As far as I can tell, you're not allowed to have the `using Foo = ...` declaration in the same declarative region as a declaration of a variable or function named `Foo`. This is because the special exception in [basic.scope.declarative]/4 only applies for "a class name or enumeration name that is *not a typedef name*". – Brian Bi Jul 20 '16 at 06:37
  • @Brian Yeah... so what about a variable of **type** `struct Foo`? And is such a variable of type `Foo` as well? I assume not. – user253751 Jul 20 '16 at 06:38
  • @immibis Oh, you are saying that you cannot use a typedef-name in an elaborated type specifier. Good point. – Brian Bi Jul 20 '16 at 06:42