Is access to the object representation using the string_view constructor (const char *, size_type) defined by char aliasing or undefined?

May we have this object

constexpr uint64_t lil_endian = 0x65'6e'64'69'61'6e; // 00en dian
    // a.k.a. Clockwise Rotated Endian which allocates like
    // char[8] = { 'n','a','i','d','n','e','\0','\0' }

It is believed that memcpythere is also a method of mutilation like this

char arr[sizeof(lil_endian)];
auto p1 = std::addressof(arr[0]);
std::memcpy(p1, &lil_endian, sizeof(lil_endian) ); // defined?
auto sv1 = std::string_view(p1, sizeof(lil_endian) );
std::string str1(sv1.crbegin()+2, sv1.crend() );
std::cout << str1        << "\n"
          << str1.size() << "\n";

everything as expected:

endian
6

The fact is that even when string_viewalone, we still appeal to the object on charwhich charone of the three possible aliases char, unsigned char, std::byte.

So, is it possible to simply skip a backup of a piece of code, for example?

auto p2 = reinterpret_cast<const char *>
                ( &lil_endian );
auto sv2 = std::string_view(p2, sizeof(lil_endian) ); // is this "char" aliasing?
std::string str2(sv2.crbegin()+2, sv2.crend() );   
std::cout << str2        << "\n"
          << str2.size() << "\n";

}

output:

endian
6

Is the constructor of string_view (const char *, size_type) "char" anti-aliasing with a strict alias ([basic.val] / 11) - § 8.2.1 ¶ 11 in N4713 p. 82

godbolt

wandbox

+4

Source: https://habr.com/ru/post/1692885/


All Articles