首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >是否可以在conexpr中使用std::string?

是否可以在conexpr中使用std::string?
EN

Stack Overflow用户
提问于 2018-04-24 01:11:49
回答 2查看 0关注 0票数 0

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

此代码失败:

代码语言:javascript
复制
constexpr std::string constString = "constString";

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

是否有可能std::string在一个constexpr?有没有其他的方法来使用字符串constexpr

EN

回答 2

Stack Overflow用户

发布于 2018-04-24 09:25:19

你可以这样做:

代码语言:javascript
复制
constexpr char constString[] = "constString";

在运行时,这可以用来在需要时构造一个std :: string。

票数 0
EN

Stack Overflow用户

发布于 2018-04-24 11:00:57

在C ++ 17中,你可以使用string_view

代码语言:javascript
复制
using namespace std::string_view_literals;

constexpr std::string_view sv = "hello, world"sv;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100003962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档