1

这是一段代码:

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.
  1. 这真的是一个错误吗?

  2. 在这种情况下,我必须提供什么我的报告?


我在在线 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。它让我知道我的代码格式不正确,但引入了更多问题:

  1. 为什么 GCC 4.5.1 报错而 GCC 4.6.0 报错?

在写完最后一段之后,我在 GCC 4.6.0 上进行了测试,去掉了static关键字,单独的实现在没有任何警告的情况下编译!

  1. 为什么同一家族的两个编译器的行为如此不同?

我知道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'
4

1 回答 1

5

这是因为这是constexpr该语言的一个新特性,并且根据GCC 页面中的 C++0x 支持,对这个特性的支持是在 4.6 中首次添加到 GCC 中的。我怀疑它实际上是编译器中的一个错误,可以在 4.6.1 或更高版本中修复。

GCC 4.5.2 会产生错误,因为该功能在该版本中尚不可用。事实上,如果您要检查 4.5.2 的标准库头文件,您会看到所有constexpr方法(由标准规定)都说类似“需要 constexpr”。

于 2011-10-09T01:01:06.780 回答