【发布时间】:2018-07-01 19:59:51
【问题描述】:
我正在尝试使用一个概念作为对子类的约束(由 gcc 使用 gnu2a 和 fconcepts 编译)来制作一个模板化继承的简单示例。我希望下面的示例可以正常编译,但我无法让它工作:
template<class structure>
concept bool Has_Type() {return requires{
typename structure::type;
};}
template<class sub> requires Has_Type<sub>()
struct structure {
//using type = typename sub::type;
};
struct child: structure<child> {
using type = child;
};
这个概念抛出一个错误,说typename structure::type would be ill-formed。我不明白为什么,因为 child 具有 :: 运算符可以访问的类型。我尝试了这个例子来看看这个想法本身是否有效,并且编译并运行良好:
struct child {
using type = child;
};
template<class it>
auto func() {
typename it::type f = child();
return 0;
}
// in a test.cpp file
auto g = func<child>();
这让我觉得这个想法得到了支持,所以我不确定为什么这个概念会失败。有人知道为什么吗?
【问题讨论】:
标签: c++ templates inheritance typename c++-concepts