I have the function hash_constexpr that takes in a const char* and returns a hash using a novel algorithm. The hash_constexpr function should be generating the hash at compile time.
namespace detail
{
template<size_t Count>
inline constexpr size_t countof(const char(&string)[Count])
{
return Count - 1;
}
template<typename T>
struct ascii_hash_t
{
template<typename L>
static constexpr T f(L const& data, T hash, size_t i = 0)
{
return i < countof(data) ? f(data, (hash & (~0u)) ^ (hash << 7) ^ T(data[i]), i + 1) : hash;
}
};
template<typename T, typename L>
inline constexpr T generate_ascii_hash(L const& data)
{
return detail::ascii_hash_t<T>::f(data, 0);
}
};
template<size_t Count>
inline constexpr uint32_t hash_constexpr(const char(&string)[Count])
{
return detail::generate_ascii_hash<uint32_t>(string);
}
My issue is that it appears that the hash_constexpr function doesn't appear to actually be returning a constexpr value. When I invoke it like so:
constexpr uint32_t asd = hash_constexpr("asdasd");
I get the following error:
Constexpr variable 'asd' must be initialized by a constant expression
What am I doing wrong?
EDIT #1:
Note that this call is working correctly:
constexpr int32_t countof_test = detail::countof("hello");
EDIT #2:
It appears that this call is working correctly as well:
constexpr int32_t generate_ascii_hash_test = detail::generate_ascii_hash<int32_t>("asd");
ascii_hash_t::fbe constexpr? - Vittorio Romeof constexpr. - Howard Hinnant