Nick
Nick

Reputation: 10539

why std::string_view is not trivial?

I want to "migrate" to C++17 and am researching it.

I found that this:

https://gcc.godbolt.org/z/sPnsEM

#include <string_view>
#include <type_traits>

int main(){
    return 
            (std::is_standard_layout_v<std::string_view> ? 10 : 20)
    +
            (std::is_trivial_v<std::string_view> ? 100 : 200)
    +       (std::is_trivially_copyable_v<std::string_view> ? 1000 : 2000)
    ;
}

returns 1210, e.g. std::string_view is standard_layout and trivially_copyable, but surprisingly is not trivial.

I checked some implementation here:

https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/experimental/string_view

and I do not see any d-tor.

Is that because of its c-tors?

Upvotes: 1

Views: 404

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473292

Your particular implementation of string_view may be trivially copyable and standard layout. But the standard does not require this of all string_view implementations. So all you're doing is testing whether your particular standard library's version has these properties.

A valid string_view implementation cannot be a Trivial type at all. The standard requires that a default-constructed string_view be empty, which requires its default constructor to be non-trivial.

Upvotes: 2

Related Questions