毕竟std :: string会最终成为我们的编译时字符串吗? [英] Will std::string end up being our compile-time string after all?

查看:61
本文介绍了毕竟std :: string会最终成为我们的编译时字符串吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多开发人员和库作者已经在编译时字符串方面苦苦挣扎了好几年了,因为标准(库)字符串 std :: string 要求动态内存分配,而不是constexpr。

Many developers and library authors have been struggling with compile-time strings for quite a few years now - as the standard (library) string, std::string, requires dynamic memory allocation, and isn't constexpr.

因此,我们有很多问题和博客文章,介绍如何正确获取编译时字符串:

So we have a bunch of questions and blog posts about how to get compile-time strings right:

  • Conveniently Declaring Compile-Time Strings in C++
  • Concatenate compile-time strings in a template at compile time?
  • C++ Compile-Time string manipulation
  • (off-site) Compile-time strings with constexpr

我们现在了解到,中不仅提供了 constexpr 代码,允许在编译时进行动态分配,但实际上 std :: string 将在C ++ 20中成为constexpr (C ++标准工作组

We've now learned that not only is new available in constexpr code, allowing for dynamical allocation at compile-time, but, in fact, std::string will become constexpr in C++20 (C++ standard working group meeting report by Herb Sutter).

这是否意味着对于C ++ 20及以上版本的代码,我们应该放弃所有那些漂亮的编译时字符串实现,而仅仅是总是使用 std :: string

Does that mean that for C++20-and-up code we should chuck all of those nifty compile-time string implementations and just always go with std::string?

如果没有-我们什么时候这样做,何时坚持

If not - when would we do so, and when would we stick to what's possible today (other than backwards-compatible code of course)?

注意:我不是谈论内容是字符串内容的字符串类型,即 not 谈论的是 std :: integral_constant 的等价物;绝对不会是 std :: string

Note: I'm not talking about strings whose contents is part of their type, i.e. not talking about the equivalent of std::integral_constant; that's definitely not going to be std::string.

推荐答案

这取决于您对 constexpr字符串的含义。

It depends on what you mean by "constexpr string".

C ++ 20允许您使用的是 std :: string 标记为 constexpr (或 consteval )的函数中。像任何文字类型一样,这样的函数可以创建 string ,对其进行操作等。但是,该字符串不能泄漏到非 constexpr 代码中。

What C++20 allows you to do is to use std::string within a function marked constexpr (or consteval). Such a function can create a string, manipulate it, and so forth just like any literal type. However, that string cannot leak out into non-constexpr code; that would be a non-transient allocation and is forbidden.

问题是,您给出的所有示例都是尝试将字符串用作模板参数。那是相似但不同的事情。您不仅在谈论在编译时构建字符串;您现在要使用它来实例化模板。

The thing is, all of the examples you give are attempts to use strings as template parameters. That's a similar-yet-different thing. You're not just talking about building a string at compile-time; you now want to use it to instantiate a template.

C ++ 20通过允许将用户定义的类型作为模板参数来解决此问题。但是,对此类类型的要求比仅仅是文字类型要严格得多。该类型必须具有没有非公开数据成员,并且唯一成员是遵循这些限制的类型。基本上,编译器需要知道其数据成员的按字节比较代表一个等效值。甚至连具有 constexpr std :: string 都不行。

C++20 solves this problem by allowing user-defined types to be template parameters. But the requirements on such types are much more strict than merely being literal types. The type must have no non-public data members and the only members are of types that follow those restrictions. Basically, the compiler needs to know that a byte-wise comparison of its data members represents an equivalent value. And even a constexpr-capable std::string doesn't work that way.

但是 std :: array< char,N> 可以做到这一点。如果您使用的是 constexpr 代码,请调用 constexpr 函数,该函数将返回 std ::字符串,并将该字符串存储在 constexpr 值中,然后存储为 string :: size() constexpr 函数。因此,您可以使用它为数组填写 N

But std::array<char, N> can do that. And if you are in constexpr code, call a constexpr function which returns a std::string, and store that string in a constexpr value, then string::size() is a constexpr function. So you can use that to fill in the N for your array.

将字符复制到 constexpr数组(因为它是一个 constexpr 的值,所以它是不可变的),但是它确实可行。

Copying the characters into a constexpr array (since it's a constexpr value, it's immutable) is a bit more involved, but it's doable.

因此C ++ 20解决了这些问题,只是没有(直接)使用 std :: string

So C++20 solves those problem, just not (directly) with std::string.

这篇关于毕竟std :: string会最终成为我们的编译时字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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