This works and is valid C++11 code, because template arguments are compile time only:
template <int x>
constexpr int do_something() {
static_assert(x > 0, "x must be > 0");
return x + 5;
}
I faced with the same problems as you did with constant expressions in C++. There's few clear documentation about constexprs at the moment. And note that there's some known bugs with it in gcc's issue tracker, but your problem seems not to be a bug.
Note that if you declare constexpr functions inside classes, you are not able to use them inside the class. This also seems to be not a bug.
Edit: This is allowed according to the standard: 7.1.3 states
... or a compound-statement that contains only
- null statements,
- static_assert-declarations
- typedef declarations and alias-declarations that do not
define classes or enumerations,
- using-declarations,
- using-directives,
- and exactly one return statement
throw
s in aconstexpr
function that is called in a constexpr context will cause the compilation to fail! – Quathlamba