static constexpr 方法实现会导致 gcc bug?
这是一段代码:
class Class
{
static constexpr int getBug();
};
constexpr int Class::getBug()
{
return 0;
}
我基本上做的是在类声明中声明一个 static
constepxr
方法,然后实现它。
原始代码被分成两个文件,并包含更多已被剥离的方法/属性,只留下所需的代码。
当我编译代码时,我从 GCC 4.6.0 收到以下错误:
Class.cpp:6:29: internal compiler error: in merge_types, at cp/typeck.c:825
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://gcc.gnu.org/bugs.html> for instructions.
这真的是一个错误吗?
- < p>在这种情况下,我必须提供报告什么?
我已经测试了代码在 在线C++0x编译器并得到以下错误:
prog.cpp:3:33: error: the 'constexpr' specifier cannot be used in a function declaration that is not a definition
prog.cpp:6:29: error: a constexpr function cannot be defined outside of its class
此编译器使用GCC 4.5.1。 它让我知道我的代码格式不正确,但引入了更多问题:
- 为什么 GCC 4.5.1 给出错误而 GCC 4.6.0 报告错误?
写完最后一段后,我测试了回到 GCC 4.6.0,剥离 static
关键字,单独的实现编译时没有任何警告!
- 为什么同一系列的两个编译器的行为如此不同?
我知道 constexpr
方法应该避免任何与 return
不同的语句code>,这可以解释 GCC 4.5.1 错误报告。 由于我的方法使用宏条件返回良好(常量)值,因此需要几行代码来解释为什么我想使用单独的实现(除了通常的建议之外)。
我的配置:
Mac OS X.7
GCC 4.6.0
Target: x86_64-apple-darwin10.7.0
Configured with: ./configure
COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.7.0' '-v' '-save-temps' '-std=c++0x' '-c' '-shared-libgcc' '-mtune=core2'
Here is a piece of code:
class Class
{
static constexpr int getBug();
};
constexpr int Class::getBug()
{
return 0;
}
What I basically do is declaring a static
constepxr
method in class declaration, then I implement it.
The original code was split in two file, and contained more methods / attributes which have been stripped, leaving only the required code.
When I compile the code I get the following error from GCC 4.6.0 :
Class.cpp:6:29: internal compiler error: in merge_types, at cp/typeck.c:825
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://gcc.gnu.org/bugs.html> for instructions.
Is this really a bug?
In that case, what must I provide my report with?
I've tested the code on an online C++0x compiler and get the following error:
prog.cpp:3:33: error: the 'constexpr' specifier cannot be used in a function declaration that is not a definition
prog.cpp:6:29: error: a constexpr function cannot be defined outside of its class
This compiler uses GCC 4.5.1.
It let me know that my code is ill-formed, but introduce more question:
- Why does GCC 4.5.1 give an error and GCC 4.6.0 report a bug?
After writing the last paragraph, I tested back on GCC 4.6.0 stripping the static
keyword and the separate implementation compiles without any warning!
- Why do two compiler of the same family behave so differently?
I know that constexpr
methods should avoid any statement different to return
, which may explain the GCC 4.5.1 error reporting.
As my method uses macro condition to return the good (constant) value, it takes a couple of line which explains why I want to use separated implementation (in addition to the usual recommendation).
My configuration:
Mac OS X.7
GCC 4.6.0
Target: x86_64-apple-darwin10.7.0
Configured with: ./configure
COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.7.0' '-v' '-save-temps' '-std=c++0x' '-c' '-shared-libgcc' '-mtune=core2'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为
constexpr
是该语言的一个新功能,并且根据 C++0x Support in GCC页面,GCC 4.6 中首次添加了对该功能的支持。我怀疑这实际上是编译器中的一个错误,可以在 4.6.1 或更高版本中修复。GCC 4.5.2 会生成错误,因为该功能在该版本中尚不可用。事实上,如果您要检查 4.5.2 的标准库标头,您会看到所有
constexpr
方法(按照标准规定)都会说“需要 constexpr”之类的内容。This is because
constexpr
is a new feature of the language, and according to the C++0x Support in GCC page, the support for this feature was first added to GCC in 4.6. My suspicion is that it is in fact a bug in the compiler, that could be fixed in 4.6.1 or later.GCC 4.5.2 generates an error because the feature isn't yet available in that version. In fact, if you were to check the standard library headers for 4.5.2, you would see that all of the
constexpr
methods (as dictated by the standard) say something like 'Needs constexpr'.