如果std :: addressof是&的可读版本.什么是*&的可读版本?

san*_*orn 1 c++ c++11 address-operator

*&x

使用c ++ 11,我们可以写为

* std::addressof(x)

但是,这个表达式有更多可读版本吗?

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

constexpr auto& arr = 
    reinterpret_cast<const std::array<char,8> &>
        (*std::addressof(lil_endian) );

int main()
{
    const auto str = std::string(arr.crbegin()+2, arr.crend() );

    std::cout << str << '\n'
              << str.size() << '\n' << '\n';
    for (const auto ch : str) {
        std::cout << ch << " : " << std::hex << (unsigned int) ch << '\n';
    }

}


endian
6

e : 65
n : 6e
d : 64
i : 69
a : 61
n : 6e
Run Code Online (Sandbox Code Playgroud)

godbolt.org/g/9StHsE

wandbox.org/permlink/ZzQ38IlDficO5UOi

Vit*_*meo 19

* std::addressof(x)

但是,这个表达式有更多可读版本吗?

x

  • @sandthorn:这是件好事,因为你所拥有的是**未定义的行为**. (5认同)

Oli*_*liv 11

维托里奥罗密欧给你第二个问题的答案.

第一个假设是错误的:"是addressof可读的版本&".addressof用于获取对象的地址,即使它的类类型有重载operator &.