这是一段代码:
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.
这真的是一个错误吗?
在这种情况下,我必须提供什么我的报告?
我在在线 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
,这可以解释 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'