gpt4 book ai didi

c++ - static_cast 的奇怪用法

转载 作者:行者123 更新时间:2023-11-30 01:50:11 28 4
gpt4 key购买 nike

我在我们的生产环境中遇到了以下代码结构(但是大大简化了)。

#include <iostream>

typedef struct
{
char entry[10];
}
inn_struct;

typedef struct
{
inn_struct directory;
}
out_struct;


struct test
{
static const int
ENTRY_LENGTH = (sizeof((static_cast<out_struct*>(0))->directory.entry)
/ sizeof((static_cast<out_struct*>(0))->directory.entry[0]));
};

int main()
{
test a;
std::cout << test::ENTRY_LENGTH;
}

现在不考虑它明显的混淆性质,因为它只是确定数组长度的旧 C 方法......我真的被 0< 的 static_cast 困扰了 值。 ... 这个代码可以接受吗?你能否在你的回复中附上一些 C++ 标准的段落,告诉我(如果)为什么这段代码没问题?

最佳答案

是的,这段代码是完全可以接受的。请参阅 §5.3.3/1(强调我的)。

The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is an unevaluated operand (Clause 5), or a paranthesized type-id.

表达式未被求值,因此看起来取消引用空指针没有问题。

另请注意,在 C++11 中,您无需跳过那个圈套,只需直接使用 sizeof 引用类成员即可,这要感谢 §5/8(重点是我的):

In some contexts, unevaluated operands appear (5.2.8, 5.3.3, 5.3.7, 7.1.6.2). An unevaluated operand is not evaluated. An unevaluated operand is considered a full-expression. [ Note: In an unevaluated operand, a non-static class member may be named (5.1) and naming of objects or functions does not, by itself, require that a definition be provided (3.2). — end note ]

和 §5.1.1/13:

An id-expression that denotes a non-static data member or non-static member function of a class can only be used:

  • ..

  • if that id-expression denotes a non-static data member and it appears in an unevaluated context [Example:

    struct S {
int m;
};

int i = sizeof(S::m); // OK
int j = sizeof(S::m + 42); // OK

- end example]

关于c++ - static_cast 的奇怪用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27840695/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com