In this comment to another question, the user hvd stated the following:
... although string literals can be passed to
constexpr
functions, and array indexing is allowed on string literals in constant expressions, an indexing operation on aconstexpr
function parameter doesn't qualify as a constant expression.
I didn't fully understand what was meant. Does it mean that the hash_value
variable in the following code
#include <cstddef>
// Compute the hash of a string literal adding the values of its characters
template<std::size_t N> constexpr std::size_t
hash_string
( const char (& s)[N] )
noexcept
{
std::size_t h = 0;
// Array indexing happening under the hood
for ( const auto c : s )
h += c;
return h;
}
constexpr auto hash_value = hash_string("Hello, world!");
cannot be evaluated at compile time? Could you elaborate on the quoted comment and tell if I am right?