c++ - 如果 std::addressof 是 & 的可读版本。 *& 的可读版本是什么?

标签 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

godbolt.org/g/9StHsE

wandbox.org/permlink/ZzQ38IlDficO5UOi

最佳答案

* std::addressof(x)

However, is there more readable version of this expression?

x

关于c++ - 如果 std::addressof 是 & 的可读版本。 *& 的可读版本是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47912719/

相关文章:

c++ - 在 C++ 中是否有任何统一的方法来创建自定义比较器?

c++ - 执行比较和交换指令时微处理器是否禁用中断?

c++ - 如何比较不同长度的元组?

c++ - 在 C++11 中调用 lambda 从高阶函数返回时出现段错误

c++ - 将 C++11 async/futures 与 Windows 异步 IO 相结合的最佳 API 是什么?

c++ - 将静态方法作为参数传递,不需要地址运算符?

pointers - Golang &(*(&v)) 语义

c++ - struct不继承虚函数

c++ - 如何获得枚举的基本类型?

c++ - to_address 的具体用例