How to tell static_assert that constexpr function arguments are const?

c++, c++11, constexpr

Solution

Is there some simple way to tell the compiler that bar is always a compile time constant?

If `bar` is always compile-time constant, then you should write your function as:

template<int bar>
constexpr int foo()
{
   static_assert(bar>arbitrary_number, "Use a lower number please");
   return something_const;
}

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_number` should be constant expression as well, or else it will not compile.

Problem

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?

Original source