san*_*orn 2 c++ strict-aliasing undefined-behavior c++20
执行C++20的严格别名规则 [basic.lval] / 11任意允许下...
char*和之间char8_t*string str = "string";
u8string u8str { (char8_t*) &*str.data() }; // c++20 u8string
u8string u8str2 = u8"zß?"
string str2 { (char*) u8str2.data() };
Run Code Online (Sandbox Code Playgroud)
uint32_t*,uint_least32_t*和char32_t*vector<uint32_t> ui32vec = { 0x007a, 0x00df, 0x6c34, 0x0001f34c };
u32string u32str { (char32_t*) &*ui32vec.data(), ui32vec.size() };
u32string u32str2 = U"zß?"
vector<uint32_t> ui32vec2 { (uint32_t*) &*u32str2.begin(),
(uint32_t*) &*u32str2.end() };
Run Code Online (Sandbox Code Playgroud)
uint16_t*,uint_least16_t*和char16_t*vector<uint16_t> ui16vec = { 0x007a, 0x00df, 0x6c34, 0xd83c, 0xdf4c };
u16string u16str { (char16_t*) &*ui16vec.data(), ui16vec.size() };
u16string u16str2 = u"zß?\ud83c\udf4c"
vector<uint16_t> ui16vec2 { (uint16_t*) &*u16str2.begin(),
(uint16_t*) &*u16str2.end() };
Run Code Online (Sandbox Code Playgroud)
basic_string建设者重载(6)
template< class InputIt >
basic_string( InputIt first, InputIt last,
const Allocator& alloc = Allocator() );
Run Code Online (Sandbox Code Playgroud)
向量构造器过载(4)
template< class InputIt >
vector( InputIt first, InputIt last,
const Allocator& alloc = Allocator() );
Run Code Online (Sandbox Code Playgroud)
我想知道与LegacyInputIterator构造函数一起使用是否合适?
char*并char8_t*作为LegacyInputIteratorstring str = "string";
u8string u8str { str.begin(), str.end() };
u8string u8str { &*str.begin(), &*str.end() };
u8string u8str2 = u8"zß?"
string str2 { u8str2.begin(), u8str2.end() };
string str2 { &*u8str2.begin(), &*u8str2.end() };
Run Code Online (Sandbox Code Playgroud)
uint32_t*,uint_least32_t*并char32_t*作为LegacyInputIteratorvector<uint32_t> ui32vec = { 0x007a, 0x00df, 0x6c34, 0x0001f34c };
u32string u32str { ui32vec.begin(), ui32vec.end() };
u32string u32str { &*ui32vec.begin(), &*ui32vec.end() };
u32string u32str2 = U"zß?"
vector<uint32_t> ui32vec2 { u32str2.begin(),
u32str2.end() };
vector<uint32_t> ui32vec2 { &*u32str2.begin(),
&*u32str2.end() };
Run Code Online (Sandbox Code Playgroud)
uint16_t*,uint_least16_t*并char16_t*作为LegacyInputIteratorvector<uint16_t> ui16vec = { 0x007a, 0x00df, 0x6c34, 0xd83c, 0xdf4c };
u16string u16str { ui16vec.begin(), ui16vec.end() };
u16string u16str { &*ui16vec.begin(), &*ui16vec.end() };
u16string u16str2 = u"zß?\ud83c\udf4c"
vector<uint16_t> ui16vec2 { u16str2.begin(),
u16str2.end() };
vector<uint16_t> ui16vec2 { &*u16str2.begin(),
&*u16str2.end() };
Run Code Online (Sandbox Code Playgroud)
该char*_t类型的线没有任何特殊的别名规则。因此,适用标准规则。这些规则在基础类型之间进行转换时也不例外。
因此,您所做的大部分是UB。不是UB的一种情况是char由于其特殊性质。实际上,您可以将的字节读取char8_t为的数组char。但是,您不能做相反的事情,将char数组的字节读取为char8_t。
现在,这些类型可以完全相互转换。因此,您可以随时将这些数组中的值转换为其他类型。
综上所述,在实际的实现中,这些事情几乎肯定会起作用。好吧,直到他们不这样做为止,因为您试图通过不应该更改的内容来更改一件事,并且编译器不会重新加载更改的值,因为它假定无法更改。所以说真的,只要使用正确,有意义的类型即可。