constexpr bool isShorter(const string &s1, const string &s2)
{
return s1.size() < s2.size();
}
编译时它说:“对非 constexpr 函数的错误调用”
最佳答案
您不能从 constexpr
函数内部调用非 constexpr
函数。而且,正如您从 here 中看到的那样, std::string::size()
不是 constexpr
。
关于C++ 为什么不能在 constexpr 函数中调用字符串大小函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31579429/