拆分定义和声明时constexpr不同的异常说明符

时间:2013-04-17 08:03:44

标签: c++ gcc c++11 constexpr noexcept

我在gcc 4.7.2上测试了以下测试代码:

#include <iostream>
#include <type_traits>

#ifdef REMOVE_CONSTEXPR_NOEXCEPT
# define CONSTEXPR_NOEXCEPT
#else
# define CONSTEXPR_NOEXCEPT noexcept
#endif

class ConstExpr {
public:
  // Some constructors
private:
  // Some member data
public:
  // Cannot split the declaration if noexcept
  static constexpr unsigned int Int(unsigned int i) CONSTEXPR_NOEXCEPT
#ifndef SPLIT_CONSTEXPR_DECLARATION
  {
    return i;
  }
#else
  ;
#endif
};

#ifdef SPLIT_CONSTEXPR_DECLARATION
constexpr unsigned int ConstExpr::Int(unsigned int i) CONSTEXPR_NOEXCEPT {
  return i;
}
#endif

class NoConstExpr {
public:
  // Some constructors
private:
  // Some member data
public:
  // Cannot split the declaration if noexcept
  static unsigned int Int(unsigned int i) noexcept;
};

// It's OK on normal functions
inline unsigned int NoConstExpr::Int(unsigned int i) noexcept {
  return i;
}

int main()
{
  std::cout << "ConstExpr: " << std::integral_constant<unsigned int,
    ConstExpr::Int(5)>::value << std::endl;
  std::cout << "NoConstExpr: " << NoConstExpr::Int(5) << std::endl;
}

我得到以下编译输出:

[matt test] g++ -std=c++11 main.cpp && ./a.out
ConstExpr: 5
NoConstExpr: 5
[matt test] g++ -std=c++11 main.cpp -DSPLIT_CONSTEXPR_DECLARATION && ./a.out
main.cpp:28:55: error: declaration of ‘static constexpr unsigned int ConstExpr::Int(unsigned int)’ has a different exception specifier
main.cpp:17:33: error: from previous declaration ‘static constexpr unsigned int ConstExpr::Int(unsigned int) noexcept (true)’
[matt test] g++ -std=c++11 main.cpp -DSPLIT_CONSTEXPR_DECLARATION -DREMOVE_CONSTEXPR_NOEXCEPT && ./a.out
ConstExpr: 5
NoConstExpr: 5

所以我的问题是:C ++ 11规范的一部分是能够拆分constexpr函数的定义和声明,还是gcc错误?

1 个答案:

答案 0 :(得分:0)

看起来gcc错误在4.8.1

中解决了这个问题