定义成员类后重新声明成员类是否合法? [英] Is it legal to re-declare a member class after defining it?

查看:130
本文介绍了定义成员类后重新声明成员类是否合法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编译boost.bimap库时遇到问题.我的测试程序是一个空白的main函数,并且只有一个include指令(如#include <boost/bimap.hpp>). 经过一番调查,我发现预处理器从头文件进行了一些有趣的构造,例如:

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; };

我不知道这是否正确,但是gcc接受它,而clang和icc不接受.谁是正确的,我该怎么做才能使用bimap库编译程序?不幸的是,在这种情况下我不能使用gcc.

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.

推荐答案

struct B{};定义了一个嵌套类,然后struct B;是对该嵌套类的重新声明.

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

GCC接受代码是错误的(错误报告),因为该标准在[class.mem]:

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,

在您的情况下,嵌套类被定义为 then 声明,这是不允许的,因此Clang和ICC可以正确地进行诊断.但是,当我对其进行测试时,它们只会发出警告,而不是错误,因此也许您正在使用-Werror,在这种情况下,请停止这样做,并且代码应编译.

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.

Boost.Bimap代码中的问题是已知错误.

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

这篇关于定义成员类后重新声明成员类是否合法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆