我試圖讓constexpr一些現有的代碼,但得到的消息文字串宣佈constexpr功能
錯誤靜:「my_string」宣佈「constexpr」靜態'功能
大大簡化,在代碼:
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.";
}
};
中的字符串用在其他地方,當然,我想,以確保它們永遠不會重複(所以靜態)(我想保持兼容性使用靜態的,其中C + +03,其中constexpr通過使用BOOST_CONSTEXPR_OR_CONST不可用)。
您是否需要使用數組而不是C字符串指針? 'const char * my_string'不夠'? – Jarod42
順便說一下,'std :: cout <<'不是'constexpr'。 – Jarod42
將變量放入函數外部的匿名名稱空間中,並對其使用BOOST_CONSTEXPR_OR_CONST cv。 – kfsone