简体   繁体   中英

Is std::string a literal type?

https://en.cppreference.com/w/cpp/named_req/LiteralType

Here they write that

Literal types are the types of constexpr variables

However it seems to me that I can't define a constexpr string (it doesn't matter if it has static storage duration or is just a stack-allocated constexpr var).

But there are the requirements for literal types:

A literal type is any of the following:

  • possibly cv-qualified class type that has all of the following properties:
    • has a [trivial (until C++20) | constexpr (since C++20)] destructor,
    • is one of
      • a type with at least one constexpr (possibly template) constructor that is not a copy or move constructor,

Does std::string satisfy these requirements? Or does it maybe fast a non-conformant destructor?

There seems to be no link to the dtor here https://en.cppreference.com/w/cpp/string/basic_string .

I understand that it can't be made fully constexpr (at least for now) because of free store allocation.

I'm just asking whether it is a literal type.

The destructor is constexpr. See the class synopsis. Thus, std::string is a literal type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM