Function and structure having the same name in C ++

The following code compiles in C ++

struct foo { int a, b; }; struct foo foo() { struct foo a; return a; } int main(void) { foo(); return 0; } 
  • Is it supposed to have a structure and function with the same name?
  • Since it compiles, I continue and try to declare an object of type foo . Is there any way? This is not possible to do:

     foo a; // error: expected ';' before 'a' foo a{}; // error: expected ';' before 'a' foo a(); // most vexing parse would kick in any way 
+7
c ++
source share
5 answers

Yes, it is allowed, we can see it by going to the draft C ++ 3.3.10 standard 3.3.10 The name hiding paragraph 2, and it says (highlighted mine):

The class name (9.1) or enumeration name (7.2) can be hidden by the variable name, data member, function, or enumerator declared in the same scope. If a class or enumeration name and a variable, data member, function, or enumerator are declared in the same scope (in any order) with the same name, the class or enumeration name is hidden wherever the variable, data member, function, or counter name is .

In this case, using struct in the declaration will fix your problem:

 struct foo a; 
+5
source share

It's usually a bad habit to do something like that. I would call it foo_s or something else to distinguish it from a function. Other than this, there really is no way to do this.

In C, this is possible, as this requires

 struct foo 

instead

Foo

as type name (if it is not typedef'd)

+1
source share

Just do it like in C using:

 struct foo a; 

You can even initialize it like this:

 struct foo a{}; struct foo a = {0}; 

The workaround will be to use typedef, while avoiding any ambiguity and other difficulties:

 typedef struct foo s_foo; 
+1
source share

Yes you can, but this is a very bad template to enter.

 struct foo { }; foo foo(foo& f) { return f; } int main() { struct foo f; foo(f); return 0; } 

See liveemo: http://ideone.com/kRK19f

The trick was to tell struct foo when we wanted to get this type. Note that until you create this ambiguity, you really don't need to keep saying struct , it's not C (as in the line foo foo(foo& f) ).

Most developers choose a camel shell pattern, for example, use an uppercase letter to indicate type names and a lowercase letter for the function name:

 struct Foo { }; Foo foo(); // no ambiguity. 

Back in Microsoft Prime, many Windows developers have acquired the habit of struct / class definition prefix, class definition of things, if you like, with capital C

 struct CFoo { }; 

Now, even if you want to use the upper letters of the first line for the names of your functions, there is no ambiguity.

+1
source share

From The.C ++. Programming.Language.Special.Edition $ A.8:

To maintain C compatibility, a class and a nonclass with the same name can be declared in the same ($ 5.7). For example:

 struct stat { /* ... */ }; int stat(char * name, struct stat * buf); 

In this case, the simple name (stat) is the name of the nonclass. The class must be assigned to use the class prefix.

0
source share

All Articles