Is it legal to re-declare a member class after def

2019-09-11 11:33发布

I have a problem with compiling boost.bimap library. My test program is a blank main function and only one include directive(like #include <boost/bimap.hpp>). After some investigations I found out that preprocessor had made some interesting constructions from header file like:

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

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

1条回答
smile是对你的礼貌
2楼-- · 2019-09-11 11:37

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

GCC is wrong to accept the code (bug report), because the standard says in [class.mem]:

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

In your case the nested class is defined then declared, which is not allowed, so Clang and ICC are correct to give a diagnostic. However, when I test it they only give a warning, not an error, so maybe you are using -Werror, in which case stop doing that and the code should compile.

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

查看更多
登录 后发表回答