In this comment to another question, the user hvd stated the following:
... although string literals can be passed to
constexprfunctions, and array indexing is allowed on string literals in constant expressions, an indexing operation on aconstexprfunction 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?
constexpr auto foo = ...;, it must be evaluated at compile time.constexpvariable means is will be evaluated at compile time and I don't see any issues with your code either.