问题描述
(最初与this question分开。)
在以下代码段中,
docker
#include <concepts>
template<
typename T,typename value_type = typename T::value_type
>
concept check_type = std::default_initializable<T>;
struct Foo {};
template<check_type T>
void func(T t) {}
int main()
{
Foo foo;
func(foo);
}
不包含类型别名struct Foo
,但编译时不会出现GCC错误。
请参见result tested on the compiler explorer。
但是,对于Clang,它会报告以下错误消息:
value_type
另请参阅result tested on the compiler explorer。
解决方法
GCC在当前措词下是正确的。
每[temp.deduct]/5,对功能模板的关联约束进行满意度检查:
如果功能模板具有关联的约束([temp.constr.decl]),则检查这些约束的满意度([temp.constr.constr])。
[temp.constr.decl]/3.2指定关联的约束基于正常形式:
声明的相关约束定义如下:
- ...
- 否则,如果仅引入一个 constraint-expression ,相关的约束是正常形式 那个表情。
由于check_type
是一个概念,因此对规范化([temp.constr.normal]/1.4)是透明的,并且由于check_type
的第二个模板参数未在其定义中使用,因此不会出现该参数以约束表达式的普通形式。因此,T::value_type
的有效性(或缺乏有效性)对满意度检查没有影响。
如果您希望概念检查value_type
,则直接直接检查value_type
更具表现力(更不用说正确了):
template <typename T>
concept check_type = std::default_initializable<T> && requires { typename T::value_type; };