gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:50:44 26 4
gpt4 key购买 nike

   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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com