C++ 为什么不能在 constexpr 函数中调用字符串大小函数

标签 c++ string c++11 size constexpr

   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/

相关文章:

java - 公共(public)领域的替代品有哪些?

c++ - 获取执行的 windbg 命令的输出

模板内部类中的 C++ decltype

python - 考虑删除获取接近的字符串匹配 - python

java - 从字符串中提取子字符串(包含动态部分)?

c++ - Xcode、C++ 和 SFML。库问题

c++ - qt C++ 命令行代码中的段错误(核心转储)

python - 使用字典和正则表达式重命名列名

c++ - c++中的<climits>和<limits>头文件有什么区别?

c++ - Linux 上的编译 - 在函数 '_start' : (. text+0x20) 中:对 'main' 的 undefined reference