可以在constexpr中使用std :: string吗? [英] Is it possible to use std::string in a constexpr?

查看:426
本文介绍了可以在constexpr中使用std :: string吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C ++ 11,Ubuntu 14.04,GCC默认工具链

此代码失败:

constexpr std::string constString = "constString";




错误:类型'const string {aka const std :: basic_string }'
constexpr变量'constString'不是文字的...因为...
'std :: basic_string'具有非平凡的析构函数

error: the type ‘const string {aka const std::basic_string}’ of constexpr variable ‘constString’ is not literal... because... ‘std::basic_string’ has a non-trivial destructor

是否可以在 constexpr 中使用 std :: string ? (显然不是...)如果是这样,怎么办?有没有其他方法可以在 constexpr 中使用字符串?

Is it possible to use std::string in aconstexpr? (apparently not...) If so, how? Is there an alternative way to use a character string in a constexpr?

推荐答案

否,编译器已经为您提供了详细的解释。

No, and your compiler already gave you a comprehensive explanation.

但是您可以这样做:

constexpr char constString[] = "constString";

在运行时,可以用来构造 std :: string

At runtime, this can be used to construct a std::string when needed.

这篇关于可以在constexpr中使用std :: string吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆