0

I have the following test piece of code tested on 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;
}

I get the following compilation output:

[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

So my question is: Is it part of the C++11 spec to be able to split the definition and declaration of constexpr functions or is this a gcc bug?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Matt Clarkson
  • 14,106
  • 10
  • 57
  • 85
  • The problem seems to be in `exception specifier`, this means - the `noexcept`, not in `constexpr`, according to me. – Kiril Kirov Apr 17 '13 at 08:12
  • You are correct in that the `noexcept` is the problem, however it only occurs on `constexpr` functions. I'll update to show that normal functions are fine. – Matt Clarkson Apr 17 '13 at 08:31
  • Probably related: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53473 – etam1024 Jul 05 '13 at 09:41

1 Answers1

0

Looks like the gcc bug solves this in 4.8.1

Matt Clarkson
  • 14,106
  • 10
  • 57
  • 85