【发布时间】:2015-01-08 12:56:21
【问题描述】:
我在我们的生产环境中遇到了以下代码结构(但是非常简化)。
#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++ 标准的段落,告诉我(如果)为什么这段代码可以?
【问题讨论】:
标签: c++ null zero static-cast