Consider this classic example:
template <typename T, std::size_t N>
constexpr std::size_t arraySize(T (&array)[N]) noexcept { return N; }
Now this works fine, but there is one annoyance, gcc gives a warning:
warning: unused parameter ‘array’ [-Wunused-parameter]
Known solutions:
- Doesn't work: If I add the classic
(void)arr;
to the function, I geterror: body of constexpr function ‘...‘ not a return-statement
. - Unsatisfactory: I can have
arraySize(T (&)[N])
, but I want to name the argument for two reasons:- It makes compiler error message more understandable.
- More subjectively, I think it makes the code clearer, especially to those who don't live and breathe that syntax.
- Not good: In this particular example, I could also
return sizeof(array)/sizeof(array[0]);
, but this approach is not universal solution, and also I thinkreturn N;
is much nicer, definitely easier on the eye. - Good but not always possible: switch to using C++14 and compiler which supports it fully. Then constexpr function body like
{ (void)array; return N; }
is allowed.
How can I get rid of the unused parameter warning nicely, when using C++11?