在对另一个问题的评论中,用户 hvd 陈述了以下内容:
...虽然可以将字符串文字传递给
constexpr
函数,并且在常量表达式中允许对字符串文字进行数组索引,但对constexpr
函数参数的索引操作不符合常量表达式的条件。
我没有完全理解是什么意思。是不是意味着hash_value
下面代码中的变量
#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!");
无法在编译时评估?您能否详细说明引用的评论并判断我是否正确?