返回constexpr的函数无法编译

sma*_*llB 11 c++ compiler-errors constexpr c++11

为什么不编译:作为返回类型
会出现问题string吗?

constexpr std::string fnc()
{
    return std::string("Yaba");
}
Run Code Online (Sandbox Code Playgroud)

R. *_*des 14

它的构造函数std::string指向char的不是constexpr.在constexpr函数中,您只能使用函数constexpr.

  • @smallB:一个常量表达式构造函数必须是1.声明`constexpr` 2.有一个成员初始化部分只涉及潜在的常量表达式而且3.有一个空体.在我看来,`std :: string`必须违反#2. (3认同)
  • +1和构造函数不能是"constexpr"的原因是它具有在编译时无法执行的副作用(即分配). (2认同)
  • @smallB:好的.我从来没有说过任何相反的话. (2认同)