'Why is std::string not trivially destructible?
I'm a c++ noob and I've been reading about trivial destructibility.
From this article on trivial destructibility,
Trivially destructible types include scalar types, trivially copy constructible classes and arrays of such types.
A trivially destructible class is a class (defined with class, struct or union) that:
- uses the implicitly defined destructor.
- the destructor is not virtual.
- its base class and non-static data members (if any) are themselves also trivially destructible types.
But apparently std::string is not trivially destructible. Why? Which of the above rules does std::string not satisfy?
std::cout << std::boolalpha
<< "std::string is trivially destructible? "
<< std::is_trivially_destructible<std::string>::value << '\n'
The above snippet returns the following output:
std::string is trivially destructible? false
Solution 1:[1]
A std::string typically contains a pointer to dynamically allocated character data, so it needs an explicit destructor to deallocate that memory. So, if nothing else, it must either fail this criterion:
uses the implicitly defined destructor
or have a base class that fails it, in which case it fails this criterion:
its base class and non-static data members (if any) are themselves also trivially destructible types.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Remy Lebeau |
