我正在尝试指定一个概念来约束具有使用 Concepts Lite 的成员函数模板的更高种类的类型。但是,我无法在技术规范或教程中找到处理概念中模板化语句的子句。
这是怎么做到的?
HKT
示例:假设我有一个带有成员函数模板的更高种类的类型F
:
template<class T>
struct HKT {
template<class U> // this looks like e.g. rebind in std::allocators
auto F(U) -> HKT<U>;
};
现在我想指定一个概念来约束这些更高种类的类型:
template <template <class> class HKT, class T>
concept HKTWithTemplateMemberFunctionF {
return requires(HKT<T> h) { // HKT<T> is a type, h is an object
// HKT<T> needs to have a member function template that
// returns HTK<U> where the type U is to be deduced and
// it can be any type (it is unconstrained)
template<class U> // is there a syntax for this?
h.F(std::declval<U>()) -> HKT<U>;
}
}
请注意,我可以执行以下操作:
template <template <class> class HKT, class T, class U>
concept HKTWithTemplateMemberFunctionF {
return requires(HKT<T> h) {
h.F(std::declval<U>()) -> HKT<U>;
}
}
但这意味着我需要U
在约束站点知道。
我真的不在乎替换给定值是否U
失败,尽管我知道为什么这可能是一个问题:例如应用约束以确保您的函数不会失败然后失败导致约束得到满足但在实例化时成员函数模板中的替换失败(如果成员函数模板受到约束,会有帮助吗?)。