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

白昼怎懂夜的黑 提交于 2019-12-13 05:46:30

问题


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:


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.



来源:https://stackoverflow.com/questions/32712057/is-it-legal-to-re-declare-a-member-class-after-defining-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!