I recently downloaded VS2015 and started tinkering around with new C++11 features. One of the features that was lacking in my game engine was a HashedString class. Since C++11 introduced constexpr, I thought a HashedString class would be a good place to start.
However, I have run into a road block with a compilation error. Whenever I try to invoke a constexpr member function from a constexpr constructor's member initializer list, the compiler complains that the member function call does not result in a constant expression. I even tried to simplify the HashString and HashStringDJB2Recursive calls to just return 0, yet, the same compile error still exists. If I remove the call to HashString, everything compiles just fine.
As far as I have researched, the member functions supplied below do not violate C++11 constexpr rules. It is possible I have missed or misunderstood something about the C++ constexpr rules. Any clarification as to why this does not compile would be much appreciated!
namespace CBConstants
{
constexpr unsigned int HASHED_STRING_HASH_CONSTANT = 5381;
}
class HashedString
{
public:
~HashedString();
explicit constexpr HashedString( const char* stringToHash ) noexcept
: mStringHash( HashString( stringToHash ) ),
mStringData( stringToHash )
{
}
private:
// DJB2 Hash documentation http://www.cse.yorku.ca/~oz/hash.html
constexpr unsigned int HashedString::HashString( const char* stringToHash ) const noexcept
{
return ( ( !stringToHash ) ? 0 : HashStringDJB2Recursive( CBConstants::HASHED_STRING_HASH_CONSTANT, stringToHash ) );
}
constexpr unsigned int HashedString::HashStringDJB2Recursive( const unsigned int hashConstant, const char* stringToHash ) const noexcept
{
return ( !(*stringToHash) ? hashConstant : HashStringDJB2Recursive(((hashConstant << 5) + hashConstant) + (*stringToHash), stringToHash + 1 ) );
}
unsigned int mStringHash;
const char* mStringData;
};
The compile error:
Error C2134 'HashedString::HashString': call does not result in a constant expression hashedstring.h 17 note: failure was caused by call of undefined function or one not declared 'constexpr'.