C++ 20概念:要求对象具有对参数有进一步要求的方法,并返回值
假设我想指定一个对象必须具有一个方法映射(...),该方法将forward_range映射到另一个(可能不同的)向前_range。我该如何在一个概念中表达这一点?
例如,应接受以下结构:
struct Mapper{
std::vector<double> map(std::list<int> args);
};
我已经看到了这个问题: c ++概念 - 我可以在课堂中有一个约束吗? 但是我不知道如何在参数和返回类型上使用更多概念,而不是使用特定类型。
编辑: 我现在也看到了这个问题: 如何在C ++ 20'中使用未指定的类型需要表达式? 但是,就我而言,这仍然没有帮助。我会更具体。
假设我想创建一组在地图上操作的操作员:
template<Map T1, Map T2>
class Chain {
const T1& map1;
const T2& map2;
public:
Chain(const T1& map1, const T2& map2)
:map1{ map1 }, map2{ map2 } {}
template<forward_range T>
auto map(T input) const {
return map2(map1(input));
}
};
template<Map T1, Map T2>
auto operator| (const T1& map1, const T2& map2) {
return Chain(map1, map2);
}
现在我想要此操作员|仅在对象上操作,该对象具有一个映射的映射函数,该函数从某种远程_range到返回值的参数映射到也是某种forward_range的返回值。
经过一项研究,我认为这有点滥用概念,作为界面,实际上应该使用地图基类来完成。但是我也不知道如何在那里的地图方法上定义这些非常通用的要求,我也想学习概念。
请注意,还有一个概念性问题,即颠覆了参数要求的强度。如果限制因素在语义上说“接受以任何形式的forward_range作为参数并返回任何类型的forward_range的映射方法的对象。”,这是一个非常严格的要求,我实际上不需要地图方法来处理地图方法任何任意的(即各种)forward_range,它接受的任何类型的论点都必须是 forwart_range。 (我希望这也解决 @Caleth的评论)
Say I want to specify that an object must have a method map(...) which maps a forward_range to another (possibly different) forward_range. How can I express that in a concept?
For instance, the following struct should be accepted:
struct Mapper{
std::vector<double> map(std::list<int> args);
};
I have seen this question:
C++ Concepts - Can I have a constraint requiring a function be present in a class?
but I don't know how to use further concepts on argument and return types instead of using specific types.
EDIT:
I have now also seen this question:
How can unspecified types be used in C++20 'requires' expressions?
However, it still does not help in my case. I will be more specific.
Let's assume I want to create a set of operators that operate on maps:
template<Map T1, Map T2>
class Chain {
const T1& map1;
const T2& map2;
public:
Chain(const T1& map1, const T2& map2)
:map1{ map1 }, map2{ map2 } {}
template<forward_range T>
auto map(T input) const {
return map2(map1(input));
}
};
template<Map T1, Map T2>
auto operator| (const T1& map1, const T2& map2) {
return Chain(map1, map2);
}
Now I want this operator| to only operate on objects, that have a map function which maps from an argument which is some kind of forward_range to a return value which is also some kind of forward_range.
After some research I think this is somewhat abusing concepts as interfaces which should actually be done with a Map base class. But I don't know how to define these very generic requirements on the map method there either and I also want to learn concepts.
Note that there is also this conceptual problem of inverting the strength of requirements on parameters. If the constraint semantically says "Accept objects with a map method that takes any kind of forward_range as argument and returns any kind of forward_range.", this is a very strict requirement and I don't actually need the map method to be able to handle any arbitrary kind of (i.e. all kinds of) forward_range, just that whatever kind of argument it accepts must be a kind of forward_range. (I hope this also addresses @Caleth's comment)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论