jeudi 22 janvier 2015

Field name same as field type using unnamed namespace

Consider this sample of code:



#include <iostream>

namespace /* unnamed namespace */
{
struct Foo
{
int a;
int b;
};
}

struct Boo
{
Foo Foo; /* field name same as field type */
int c;
void print();
};

void Boo::print()
{
std::cout<<"c = "<<c<<std::endl;
std::cout<<"Foo "<<Foo.a<<" "<<Foo.b<<std::endl;
}

int main()
{
Boo boo;
boo.c=30;
boo.Foo.a=-21;
boo.Foo.b=98;
boo.print();
return 0;
}


Clang can compile it without errors.


Debian clang version 3.5.0-9 (tags/RELEASE_350/final) (based on LLVM 3.5.0)


Microsoft cl.exe compile it without errors. (I don't remember version. I use VS 2012)


And GCC: gcc version 4.9.2 (Debian 4.9.2-10):



main.cpp:14:6: error: declaration of ‘{anonymous}::Foo Boo::Foo [-fpermissive]
Foo Foo; /* field name same as field type */
^
main.cpp:5:9: error: changes meaning of ‘Foo’ from ‘struct {anonymous}::Foo’[-fpermissive]
struct Foo
^


What is good behavior of compiler? Why GCC can't compile it, but clang and cl.exe does? What C++ standard says?


Aucun commentaire:

Enregistrer un commentaire