gpt4 book ai didi

c++ - 函数引用 sizeof 是可移植的吗?

转载 作者:搜寻专家 更新时间:2023-10-31 00:22:48 25 4
gpt4 key购买 nike

在寻找一种方法来为 c 风格的数组进行可移植、安全的元素计数时,我找到了这个解决方案:

template <typename T, unsigned N>  char (&arrayCountofHelper(T(&)[N]))[N];
#define ARRAY_COUNTOF(arr) (sizeof(arrayCountofHelper(arr)))

arrayCountofHelper 似乎实际上是对函数的引用,宏 ARRAY_COUNTOF 使用函数的大小是其返回类型的大小这一事实。
它工作得很好。
然而,当我试图检查它是否可移植时,我没有找到任何证据。我在标准 (14882/1998) 中找不到任何对 sizeof(function reference) 的引用,事实上,我不完全确定我什至可以再创建一个函数引用(尽管该标准确实提到了几个次)。
那么,有人知道我应该看标准中的什么地方吗? (或者,如果我以某种方式误解了声明,那么正确的解释是什么?)

谢谢
奥伦

附言(对于那些认为我没有找到适合我的问题的解决方案的人)
我知道我可以随时使用

#define ARRAY_COUNTOF sizeof(arr)/sizeof(arr[0])

甚至

template <typename T, unsigned N> size_t arrayCountof(T(&)[N]) {return N;}

但第一个不会检查 arr 是否为数组(或指针),第二个在 static_assert 中不可用。
(我会使用 std::tr1::array 或 std::vector,但这是我正在维护的遗留代码)

最佳答案

我们将松散地把它分开,我使用的是 INCITS+ISO+IEC+14882-2003。我会引用一些小的东西,但一些更复杂的东西太大而无法引用。

sizeof在 §5.3.3 中定义,它说(删节):

The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is not evaluated, or a parenthesized type-id.

换句话说,它产生一个类型的大小(以字节为单位)或找到一个表达式的类型并产生它的大小。我们没有类型,我们有表达式 arrayCountofHelper(arr) .

您可以通过查看 primary-expression 的定义来剖析此表达式和 postfix-expression分别在 §5.1 和 §5.2 中定义。你会发现它是一个 postfix-expression并符合函数调用的要求(§5.2.2)。

现在回到sizeof .我们只关心这个函数调用表达式的类型(所以我们可以产生它的大小),§5.2.2/3 说:

The type of the function call expression is the return type of the statically chosen function [...]. This type shall be a complete object type, a reference type or the type void.

因此我们需要使用 arrayCountofHelper(arr) 找到将被调用的函数的返回类型(记住,这都是未计算的) . arrayCountofHelper是一个函数模板,我们将对其进行实例化(第 14.7 节),因此我们需要在实例化函数之前执行此操作以获取其返回类型。

所有模板参数都需要有值(§14.8.2),通过使用§14.8.2.1 中定义的规则,我们将找到 TN通过将传递给函数的数组与函数参数(这是对数组的引用)进行匹配。 (例如,如果 arrint[10]T 将是 intN 将是 10。)一旦我们有了这些,就可以实例化该函数。

一旦实例化,函数的返回类型将为char(&)[N] *,对 N 数组的引用char的。 (如果您需要解析方面的帮助,请参阅§8.3.5。关于如何解析“复杂”类型的 SO 也有问题。)所以现在我们已经找到了表达式的类型,我们必须获取它的大小。

§5.3.3/2 定义了如何 sizeof使用引用和数组(强调我的):

When applied to a reference or a reference type, the result is the size of the referenced type. When applied to a class, the result is the number of bytes in an object of that class including any padding required for placing objects of that type in an array. The size of a most derived class shall be greater than zero (1.8). The result of applying sizeof to a base class subobject is the size of the base class type.70) When applied to an array, the result is the total number of bytes in the array. This implies that the size of an array of n elements is n times the size of an element.

引用类型的大小是其引用类型的大小,所以我们需要char[N]的大小.这个的大小是N * sizeof(char) . char是最基本的,因为它是最小的类型;即 sizeof(char) 总是一个。 (§5.3.3/1) 所以这个表达式产生的大小是 1 * N ,或者我们一直想要的:N .

这就是它的工作原理。


这个比你最后一个例子 arrayCountof 更受欢迎的原因是因为 sizeof 的结果是一个常量表达式,所以可以用在需要常量表达式的地方。

应该注意的是,在 C++0x 中,我们可以通过以下方式获得干净的无宏语法:

template <typename T, unsigned N>
constexpr size_t arrayCountof(T(&)[N]) {return N;}

* 函数返回类型是对数组而不是数组的引用的原因很简单,因为您不能返回数组。如果可以,任何一个选择都可以。

关于c++ - 函数引用 sizeof 是可移植的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2944794/

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