gpt4 book ai didi

c++ - friend 模板特化声明中不允许使用 Consexpr?

转载 作者:IT老高 更新时间:2023-10-28 12:39:31 25 4
gpt4 key购买 nike

我正在将 C++14-constexpr 代码库从 Clang 移植到最新的 g++-5.1。考虑以下自 Clang 3.3 的 Restful 时期以来一直正确编译的本土 bitset 类的简化代码片段(现在快 2 年了!)

#include <cstddef>

template<std::size_t>
class bitset;

template<std::size_t N>
constexpr bool operator==(const bitset<N>& lhs, const bitset<N>& rhs) noexcept;

template<std::size_t N>
class bitset
{
friend constexpr bool operator== <>(const bitset<N>&, const bitset<N>&) noexcept;
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ <-- error from this piece
};

template<std::size_t N>
constexpr bool operator==(const bitset<N>& /* lhs */, const bitset<N>& /* rhs */) noexcept
{
return true;
}

int main() {}

Live example在魔杖盒上。但是,g++-5.1 和当前的主干版本给出了错误:

'constexpr' is not allowed in declaration of friend template specialization

问题:这是已知的 g++ 错误还是 Clang 不符合最新标准?

注意:上面只使用了 C++11 风格的 constexpr 特性,因为 operator== 内部没有进行任何修改,所以模板、 friend 和 constexpr 之间似乎有些奇怪的干扰。

更新:归档为 bug 65977在 Bugzilla 上。

最佳答案

这里的 GCC 是错误的。

所有引用均指向最新的 C++ WD N4431。

[tl;dr:函数是 inline(或更准确地说,是一个 inline 函数,如 7.1.2/2 中所定义)和使用 inline 说明符声明。 constexpr 说明符使函数内联,但不是 inline 说明符。]

说明符在 C++ 标准的子条款 7.1 中描述,并且是语法的一个元素。因此,每当标准谈到 foo 说明符出现在某处时,这意味着说明符字面上出现在源代码的(分析树)中。 inline 说明符是一个 function-specifier,在子条款 7.1.2 中描述,其作用是使函数成为内联函数。 (7.1.2)/2:

A function declaration (8.3.5, 9.3, 11.3) with an inline specifier declares an inline function.

还有另外两种声明内联函数的方法,无需使用 inline 说明符。一个在 (7.1.2)/3 中有描述:

A function defined within a class definition is an inline function.

另一个在 (7.1.5)/1 中描述:

constexpr functions and constexpr constructors are implicitly inline (7.1.2).

这些都不是说这种行为就像一个 inline specifier 存在,只是这个函数是一个内联函数。

那么为什么会有这条规则呢?

在 (7.1.2)/3 中有此规则的更简单形式:

If the inline specifier is used in a friend declaration, that declaration shall be a definition or the function shall have previously been declared inline.

这样做的目的是在大多数情况下允许忽略友元声明——不允许将“新信息”添加到友元实体,除非在定义友元函数的特殊情况下。 (这反过来又允许实现延迟解析类定义,直到它“需要”。)因此我们也看到,在 (8.3.6)/4 中:

If a friend declaration specifies a default argument expression, that declaration shall be a definition and shall be the only declaration of the function or function template in the translation unit.

这同样适用于函数模板的友元特化声明:如果它可以添加额外信息,那么实现就不会延迟解析类定义。

现在,请注意这个基本原理适用于 constexpr:如果 constexpr 说明符出现在函数的任何声明中,它必须根据 (7.1.5)/1,出现在每个声明中。由于这里没有“新信息”,所以不需要限制。

关于c++ - friend 模板特化声明中不允许使用 Consexpr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29871138/

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