Is it legal to re-declare a member class after it is defined?

I had a problem compiling the boost.bimap library. My test program is an empty main function, and only one includes a directive (for example, #include <boost/bimap.hpp> ). After some research, I found out that the preprocessor made some interesting constructions from the header file, for example:

 struct A { struct B{}; struct B; }; 

I don't know if this is correct or not, but gcc accepts it, but clang and icc do not. Who is right and what can I do to compile programs with a bimap library? Unfortunately, I cannot use gcc in this case.

0
source share
1 answer

struct B{}; defines a nested class, then struct B; is a re-declaration of the same nested class.

GCC mistakenly accepts the code ( error report ) because the standard says in [class.mem]:

A member must not be declared twice in the member specification, except that a template of a nested class or a member class can be declared and then defined later,

In your case, the nested class is defined and then declared, which is not valid, which is why Clang and ICC are correct for diagnostics. However, when I check this, they give a warning, not an error, so maybe you are using -Werror , in which case stop doing this, and the code should compile.

The problem in Boost.Bimap code is a known bug .

+10
source

Source: https://habr.com/ru/post/1232022/


All Articles