【发布时间】:2017-12-20 18:59:37
【问题描述】:
*&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
【问题讨论】:
-
通过
arr访问lil_endian是未定义的行为。另外,你想做什么?*&x用于“普通类型”是一个 noop -
@Barry 我不知道如何解释这一点,但请尝试在上面的上帝螺栓链接上将
*&x替换为x。 -
& 和 addressof 通常不相等
-
你觉得
std::addressof更多可读吗? -
@sandthorn 您的上帝螺栓链接使用
lil_endian、*&lil_endian或*std::addressof(lil_endian)生成完全相同的程序集。
标签: c++ c++11 address-operator