Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7944 Discussions

anonymous union with an anonymous struct

wang_p_1
Beginner
1,730 Views

Hello,
this is my compiler's information:
ICPC Intel(R) 64, Version 14.0.2.144 Build 20140120


the source code example is not intricacy:

struct vect3{
        int x,y,z;
        vect3(int _x,int _y,int _z):x(_x),y(_y),z(_z){ }
};
class vect
{
        union{
                int leaf[9];
                struct{
                        vect3 a;
                        vect3 b;
                        vect3 c;
                };
        };
public:
        vect(int _a,int _b,int _c):a(_a,_b,_c),b(_a,_b,_c),c(_a,_b,_c){  }
        vect3 getXcoordinate(){return a;}
        vect3 getYcoordinate(){return b;}
        vect3 getZcoordinate(){return c;}
};
int main()
{
        vect v1=vect(1,2,3);
        std::cout<<"X is: "<<v1.getXcoordinate().x<<"\tY is: "<<v1.getYcoordinate().y<<std::endl;
        return 0;
}

this code can be compiled by visual studio 2008' s compiler.

but, icpc reports: invalid member for anonymous member class -- class "vect3" has a disallowed member function  vect3 a;

I know the error comes from the anonymous struct in an anonymous union. But i just want to use it in that way. How can I do ?

0 Kudos
1 Solution
Shenghong_G_Intel
1,730 Views

Hi Wang P,

I have verified the test case on Linux. And yes I can reproduce the issue you reported. However, even G++ has the same error, without C++11. Adding C++ 11, g++ still have some errors (less), but ICPC can pass the test case. Looks like there are some issue for G++ even in c++11 mode. could you please check whether you are testing on Linux? If yes, you may need to enable c++11. Let me know whether it works.

Thanks,

Shenghong

# g++ temp.cpp
temp.cpp:12:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::a' with constructor not allowed in anonymous aggregate
                         vect3 a;
                               ^
temp.cpp:13:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::b' with constructor not allowed in anonymous aggregate
                         vect3 b;
                               ^
temp.cpp:14:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::c' with constructor not allowed in anonymous aggregate
                         vect3 c;
                               ^
temp.cpp:12:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::a' with constructor not allowed in union
                         vect3 a;
                               ^
temp.cpp:12:31: note: unrestricted unions only available with -std=c++11 or -std=gnu++11
temp.cpp:13:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::b' with constructor not allowed in union
                         vect3 b;
                               ^
temp.cpp:14:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::c' with constructor not allowed in union
                         vect3 c;
                               ^
# g++ temp.cpp -std=c++11
temp.cpp:12:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::a' with constructor not allowed in anonymous aggregate
                         vect3 a;
                               ^
temp.cpp:13:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::b' with constructor not allowed in anonymous aggregate
                         vect3 b;
                               ^
temp.cpp:14:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::c' with constructor not allowed in anonymous aggregate
                         vect3 c;
                               ^
# icc temp.cpp
temp.cpp(12): error #1764: invalid member for anonymous member class -- class "vect3" has a disallowed member function
                          vect3 a;
                                ^

temp.cpp(13): error #1764: invalid member for anonymous member class -- class "vect3" has a disallowed member function
                          vect3 b;
                                ^

temp.cpp(14): error #1764: invalid member for anonymous member class -- class "vect3" has a disallowed member function
                          vect3 c;
                                ^

compilation aborted for temp.cpp (code 2)
# icc temp.cpp -std=c++11
# ./a.out
X is: 1 Y is: 2
#

 

View solution in original post

0 Kudos
3 Replies
Shenghong_G_Intel
1,730 Views

Hi Wang P,

I cannot reproduce the issue with both 14.0 and 15.0 compiler, with ia32 and intel64.

Also, I have one concern, could you please double confirm the version of your compiler? is it an official release version? I cannot find any release version with a build date of 20140120 for 14.0 Update 2 release. You may also provide your product name???

Below is an article about the version mapping...looks like the Linux version has "14.0.2.144 / 20140120", please help to double confirm...I will also try to test the code on Linux.

https://software.intel.com/en-us/articles/intel-compiler-and-composer-update-version-numbers-to-compiler-version-number-mapping

Thanks,

Shenghong

0 Kudos
Shenghong_G_Intel
1,731 Views

Hi Wang P,

I have verified the test case on Linux. And yes I can reproduce the issue you reported. However, even G++ has the same error, without C++11. Adding C++ 11, g++ still have some errors (less), but ICPC can pass the test case. Looks like there are some issue for G++ even in c++11 mode. could you please check whether you are testing on Linux? If yes, you may need to enable c++11. Let me know whether it works.

Thanks,

Shenghong

# g++ temp.cpp
temp.cpp:12:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::a' with constructor not allowed in anonymous aggregate
                         vect3 a;
                               ^
temp.cpp:13:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::b' with constructor not allowed in anonymous aggregate
                         vect3 b;
                               ^
temp.cpp:14:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::c' with constructor not allowed in anonymous aggregate
                         vect3 c;
                               ^
temp.cpp:12:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::a' with constructor not allowed in union
                         vect3 a;
                               ^
temp.cpp:12:31: note: unrestricted unions only available with -std=c++11 or -std=gnu++11
temp.cpp:13:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::b' with constructor not allowed in union
                         vect3 b;
                               ^
temp.cpp:14:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::c' with constructor not allowed in union
                         vect3 c;
                               ^
# g++ temp.cpp -std=c++11
temp.cpp:12:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::a' with constructor not allowed in anonymous aggregate
                         vect3 a;
                               ^
temp.cpp:13:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::b' with constructor not allowed in anonymous aggregate
                         vect3 b;
                               ^
temp.cpp:14:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::c' with constructor not allowed in anonymous aggregate
                         vect3 c;
                               ^
# icc temp.cpp
temp.cpp(12): error #1764: invalid member for anonymous member class -- class "vect3" has a disallowed member function
                          vect3 a;
                                ^

temp.cpp(13): error #1764: invalid member for anonymous member class -- class "vect3" has a disallowed member function
                          vect3 b;
                                ^

temp.cpp(14): error #1764: invalid member for anonymous member class -- class "vect3" has a disallowed member function
                          vect3 c;
                                ^

compilation aborted for temp.cpp (code 2)
# icc temp.cpp -std=c++11
# ./a.out
X is: 1 Y is: 2
#

 

0 Kudos
wang_p_1
Beginner
1,730 Views

Hi shenghong:

   Thanks for your reply. It does work.

   I'm so sorry about my carelessness. I should write down my compiler's instruction: #icpc test.cpp -o test

  I have forgotten to enable C++11. The  anonymous struct is  a part  standard of C++11.

 

 

 

shenghong-geng (Intel) wrote:

Hi Wang P,

I have verified the test case on Linux. And yes I can reproduce the issue you reported. However, even G++ has the same error, without C++11. Adding C++ 11, g++ still have some errors (less), but ICPC can pass the test case. Looks like there are some issue for G++ even in c++11 mode. could you please check whether you are testing on Linux? If yes, you may need to enable c++11. Let me know whether it works.

Thanks,

Shenghong

# g++ temp.cpp
temp.cpp:12:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::a' with constructor not allowed in anonymous aggregate
                         vect3 a;
                               ^
temp.cpp:13:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::b' with constructor not allowed in anonymous aggregate
                         vect3 b;
                               ^
temp.cpp:14:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::c' with constructor not allowed in anonymous aggregate
                         vect3 c;
                               ^
temp.cpp:12:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::a' with constructor not allowed in union
                         vect3 a;
                               ^
temp.cpp:12:31: note: unrestricted unions only available with -std=c++11 or -std=gnu++11
temp.cpp:13:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::b' with constructor not allowed in union
                         vect3 b;
                               ^
temp.cpp:14:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::c' with constructor not allowed in union
                         vect3 c;
                               ^
# g++ temp.cpp -std=c++11
temp.cpp:12:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::a' with constructor not allowed in anonymous aggregate
                         vect3 a;
                               ^
temp.cpp:13:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::b' with constructor not allowed in anonymous aggregate
                         vect3 b;
                               ^
temp.cpp:14:31: error: member 'vect3 vect::<anonymous union>::<anonymous struct>::c' with constructor not allowed in anonymous aggregate
                         vect3 c;
                               ^
# icc temp.cpp
temp.cpp(12): error #1764: invalid member for anonymous member class -- class "vect3" has a disallowed member function
                          vect3 a;
                                ^

temp.cpp(13): error #1764: invalid member for anonymous member class -- class "vect3" has a disallowed member function
                          vect3 b;
                                ^

temp.cpp(14): error #1764: invalid member for anonymous member class -- class "vect3" has a disallowed member function
                          vect3 c;
                                ^

compilation aborted for temp.cpp (code 2)
# icc temp.cpp -std=c++11
# ./a.out
X is: 1 Y is: 2
#

 

0 Kudos
Reply