static_cast 的奇怪用法

Strange usage of static_cast

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

#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 引用 class 成员即可,感谢 §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]