【问题标题】:GNU version issue with gnu++0x standardgnu++0x 标准的 GNU 版本问题
【发布时间】:2013-08-01 10:46:23
【问题描述】:

我需要用两个编译器版本编译代码:

g++ (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3

g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3

我在这样的头文件中有一段代码:

template <RealType> class Constant {
   ...
   /*constexpr*/ static const RealType Pi = 3.1415926535897932384626433832795028841971693993751;
   ...
};

如果我使用 constexprg++ -std=gnu++0x 构建代码,它适用于版本 4.7.3。但是使用 4.6.3 版本。构建包含头文件的 *.cpp 文件失败:

error: both ‘const’ and ‘constexpr’ cannot be used here

但是,如果我不使用 constexpr 版本 4.6.3 抱怨:

error: ‘constexpr’ needed for in-class initialisation of static data member ‘const double Constant<double>::Pi’

有解决办法吗?

顺便说一句,如果我省略-std=gnu++0x(当然还有constexpr),代码构建得很好

【问题讨论】:

  • 当您说“代码构建良好”时,您确定启用所有警告和-pedantic
  • 您意识到 GCC 4.6.3 支持的 C++11 少于 4.7.3,是吗?
  • @NicolBolas 是的,我愿意。但还有其他因素。这是一个更大的项目。

标签: c++ c++11 standards gnu


【解决方案1】:

const 对于变量来说是多余的。就constexpr,像这样:

static constexpr RealType Pi = 3.14;



在 C++14 中,您甚至可以说,

template <typename T> constexpr Pi = T(3.14);

并将其用作Pi&lt;double&gt; 等。

【讨论】:

  • 我想,我本可以自己解决这个问题。谢谢。
猜你喜欢
  • 2012-05-15
  • 1970-01-01
  • 1970-01-01
  • 2017-11-09
  • 2013-05-09
  • 1970-01-01
  • 2013-04-14
  • 1970-01-01
  • 2021-10-17
相关资源
最近更新 更多