2

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.

deshalder
  • 507
  • 2
  • 13
  • 2
    the destructor is not trivial, it has to perform cleanup actions if memory was allocated. – NathanOliver Jun 21 '22 at 21:51
  • Maybe it's constexpr then? :) – deshalder Jun 21 '22 at 21:53
  • 5
    Related: [constexpr std::string in C++20, how does it work?](https://stackoverflow.com/questions/70571655/) – Remy Lebeau Jun 21 '22 at 22:08
  • Although cppreference's page for `basic_string` doesn't link to a description of the destructor, that doesn't mean the destructor is trivial. It means the purpose of the destructor is pretty well specified (in this case, it destroys the string object and releases any internal storage the object uses). However, constructors and other member functions of `basic_string` may dynamically allocate or reallocate memory (using the supplied allocator) so the destructor must release any such memory. Hence the destructor is non-trivial, and `basic_string` is not a literal type. – Peter Jun 22 '22 at 07:40
  • @peter but the destructor /is/ constexpr so it is a literal type. – Jeff Garrett Jun 23 '22 at 02:02

1 Answers1

2

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

Jeff Garrett
  • 5,863
  • 1
  • 13
  • 12