简体   繁体   中英

C++ VS2015 constexpr compile error with constexpr constructor call to constexpr member function

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'.

Your class is not a literal because you have a non-trivial destructor.

According to the C++ standard

12.4/5 A destructor is trivial if it is not user-provided and if:

— the destructor is not virtual,

— all of the direct base classes of its class have trivial destructors, and

— for all of the non-static data members of its class that are of class type (or array thereof), each such class has a trivial destructor.

Otherwise, the destructor is non-trivial.

In your case you declare the destructor, so the destructor is non trivial. If you do

~HashedString() = default;

or even don't declare it, the code should compile.

Live example on Coliru

EDIT

VC++ seems to care about the order of definition of your functions. Moving them above the constructor will make the code compilable (although this is not mandated by the C++ standard).

Live example on rextester VC++

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM