C ++ VS2015 constexpr编译错误,constexpr构造函数调用constexpr成员函数

时间:2015-11-25 01:48:52

标签: c++ string c++11 hash visual-studio-2015

我最近下载了 VS2015 并开始修补新的 C ++ 11 功能。我的游戏引擎中缺少的一个功能是HashedString类。由于C ++ 11引入了 constexpr ,我认为HashedString类是一个很好的起点。

但是,我遇到了编译错误的路障。每当我尝试从constexpr构造函数的成员初始化列表中调用constexpr成员函数时,编译器都会抱怨成员函数调用不会导致常量表达式。我甚至试图简化HashString和HashStringDJB2Recursive调用只返回0,但是,仍然存在相同的编译错误。如果我删除对HashString的调用,一切都编译得很好。

据我所研究,下面提供的成员函数不违反C ++ 11 constexpr规则。我可能错过或误解了有关C ++ constexpr规则的一些内容。任何有关为什么不编译的澄清将非常感激!

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;

};

编译错误:

  

错误C2134'HashedString :: HashString':调用不会导致常量表达式hashedstring.h 17   注意:失败是由调用未定义的函数或未声明为'constexpr'的函数引起的。

1 个答案:

答案 0 :(得分:2)

你的班级不是literal,因为你有一个非平凡的析构函数。

根据C ++标准

  

12.4 / 5如果不是用户提供并且如果:

,则析构函数是微不足道的      

- 析构函数不是虚拟的,

     

- 其类的所有直接基类都很简单   析构函数,

     

- 对于其类的所有非静态数据成员   类类型(或其数组),每个这样的类都有一个小问题   析构函数。

     

否则,析构函数是非常重要的。

在你的情况下,你声明析构函数,所以析构函数是非常重要的。如果你这样做

~HashedString() = default;

或者甚至没有声明它,代码应该编译。

Live example on Coliru

修改

VC ++似乎关心函数定义的顺序。将它们移到构造函数上方将使代码可编译(尽管这不是C ++标准的强制要求)。

Live example on rextester VC++