假设我正在编写一个类模板,其中一些成员的存在受到限制和价值类型模板参数 static constexpr 数据成员的:
template<class T>
struct A {
constexpr bool operator()() requires T::value { return T::value; }
constexpr bool operator()() { return false; }
};
#include <type_traits>
static_assert(A<std::true_type>()());
static_assert(!A<std::false_type>()());
static_assert(!A<void>()());
MSVC 和 gcc 接受这一点,但 clang 拒绝,除非我replace requires T::value
with requires requires { requires T::value; }
。这是 clang 中的错误,还是其他编译器松懈? C++ 是否需要需要需要需要?标准怎么说?
相关(嗯,至少 ⅔):为什么我们需要需要需要?
这是一个 clang bug(已提交#49513).
看起来 clang 正在替代void
into T::value
并决定因为这是一个无效的表达式,所以约束是invalid。但规则,在[温度构造原子]就是它:
为了确定是否满足原子约束,首先将参数映射和模板实参替换到其表达式中。如果替换导致无效类型或表达式,则不满足约束。
在这种情况下,替换会导致无效的类型或表达式,因此其结果应该是不满足约束。
请注意,此重载:
constexpr bool operator()() requires T::value { return T::value; }
仅在以下情况下有效T::value
是一个有效的表达式and评估为true
。这使得它相当于:
constexpr bool operator()() requires T::value { return true; }
在这种情况下这很好,因为你要回来false
无论如何,在另一种情况下,所以没有理由区分T::value
存在但是是false
from T::value
不存在。
但认为无论如何还是值得澄清的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)