小编Pau*_*tow的帖子

在 constexpr 函数中声明为静态的文字字符串

我正在尝试使 constexpr 一些现有代码,但收到消息

错误:“my_string”在“constexpr”函数中声明为“static”

简化了很多,代码如下:

template <typename T>
constexpr
int foo(const int x)
{
  static // error: 'my_string' declared 'static' in 'constexpr' function
  constexpr char my_string[] = "my foo error message!";
  if (x == 0)
  {
    std::cout << my_string << std::endl;
  }
  return  x;
}

class boo
{
public:
  constexpr boo()
  {
   static // error: 'constructor_string' declared 'static' in 'constexpr' function
   constexpr char constructor_string[] = "my constructor error message.";
  }
};
Run Code Online (Sandbox Code Playgroud)

字符串当然在其他地方使用,我想确保它们永远不会重复(如此静态)(并且我想保持使用静态以与 C++03 兼容,其中 constexpr 不可用使用BOOST_CONSTEXPR_OR_CONST)。

c++ string static literals constexpr

3
推荐指数
1
解决办法
1650
查看次数

标签 统计

c++ ×1

constexpr ×1

literals ×1

static ×1

string ×1