I have a constexpr function that looks something like this:
constexpr int foo(int bar)
{
static_assert(bar>arbitrary_number, "Use a lower number please");
return something_const;
}
However, compiling this with GCC 4.6.3 keeps telling me
error: ‘bar’ cannot appear in a constant-expression
I tried something like
constexpr int foo(constexpr const int bar)
{
static_assert(bar>arbitrary_number, "Use a lower number please");
return something_const;
}
but constexpr can’t be used for function arguments.
Is there some simple way to tell the compiler that bar is always a compile time constant?
If
baris always compile-time constant, then you should write your function as:Because if you don’t do so, and instead write what you’ve already written, then in that case, the function can be called with non-const argument as well; it is just that when you pass non-const argument, then the function will loss it’s constexpr-ness.
Note that in the above code
arbitrary_numbershould be constant expression as well, or else it will not compile.