TS 概念类型名约束
TS Concepts typename constraint
我试图使用概念作为子类的约束(由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
.我不明白为什么,因为孩子有一个可以通过::
运算符访问的类型。我尝试了这个例子,看看这个想法本身是否有效,这编译并运行良好:
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>();
这让我认为这个想法得到了支持,所以我不确定为什么这个概念失败了。有人知道为什么吗?
这是因为child
在这一点上是不完整的。[class.mem]p6 说:
类在 类说明符。
后跟一些例外(例如不在成员函数中(。但是在基句中,它是不完整的,因此成员type
对Has_Type
不可用。
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 函数作为模板参数,是否对返回类型强制约束
- 如果原型是本地的,则使用流 I/O C++类型约束将失败
- 约束类模板函数以接受特定的 POD 类型
- TS 概念类型名约束
- 如何在带有约束 (C++) 的函数中使用泛型类型
- 文本类类型成员函数约束
- 如何约束参数类型以仅允许 std::initializer_list<size_t> 或 std::array<size_t, N>?
- 约束类型模板参数仅对特定模板进行实例化
- 为白名单类型约束模板函数的更好方法,SFINAE
- 作为模板参数的可调用类型上的C++约束
- 模板类型的约束
- 如何将类型约束和隐式转换与C++11通用引用相结合?
- c++调用约定是否受标准约束,因为在声明fn时不需要定义函数的返回类型
- 为什么std::copy_if签名不约束谓词类型
- 需要关于c++ v11中类模板中的类型约束设计的建议