我正在尝试指定一个概念来约束具有使用Concepts Lite的成员函数模板的高级类型。但是,我在technical specification或tutorial中找不到处理概念中模板化语句的子句。
这是怎么做的?
示例:假设我有一个具有成员函数模板F的高类类型HKT
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>;
}
}但这意味着我需要知道constraint site的U。
我真的不关心对给定U的替换是否失败,尽管我可以看到这可能是一个问题的原因:例如,应用约束以确保您的函数不会失败,然后失败会导致约束得到满足,但在实例化时,成员函数模板中的替换失败(如果成员函数模板受到约束会有帮助吗?)。
发布于 2014-12-17 17:45:39
长话短说,现在你(我?)必须提供特定的U
template <template <class> class HKT, class T, class U = T>
concept HKTWithTemplateMemberFunctionF {
return requires(HKT<T> h) { // HKT<T> is a type, h is an object
h.F(std::declval<U>()) -> HKT<U>;
}
}由于编译器不能证明可能存在的所有类型的U都可以使用成员函数模板,也就是说,以下情况是没有希望的:
template <template <class> class HKT, class T>
concept HKTWithTemplateMemberFunctionF {
return requires(HKT<T> h) {
template<class U> // for all those Us that haven't been written yet...
h.F(std::declval<U>()) -> HKT<U>;
}
}在一个假想的5分钟内设计的概念--轻量级实现中,我们可以对U进行一点限制:
template <template <class> class HKT, class T,
InputIterator U = InputIterator() /* imaginary syntax */ >
concept HKTWithTemplateMemberFunctionF {
return requires(HKT<T> h) {
h.F(std::declval<U>()) -> HKT<U>; // Is InputIterator enough to instantiate F?
}
}编译器只需要检查InputIterator的模型是否足以实例化h.F,即使h.F不受约束也是可能的!此外,提供U只需检查它是否模拟了InputIterator,甚至不需要尝试检查U的h.F,因为InputIterator就足够了。这可以用来优化编译时的性能和...
...would可能会以令人惊讶的方式与SFINAE交互,因为AFAIK你可以有一个概念重载的函数(例如,对于InputIterator),它可以接受除一个(SFINAE!为什么会有人这样做?!),因此可以通过概念检查,但在实例化时失败...悲伤的。
https://stackoverflow.com/questions/23659382
复制相似问题