我想创建一个概念Handler
,它将检查实现者是否能够接受任何满足Update
概念的类型, -而实际上不必用我拥有的每一个可能的update对象示例化这个概念。
template <typename T>
concept Update = requires (T update) {
{ update.get_value(); } -> std::same_as<std::string>;
...
};
template <typename T>
concept Handler = requires (T handler, Update update) {
handler.on_update(update);
};
然而,除非我给Handler
概念添加一个额外的参数,并传递Update
概念的一个特定示例,否则这实际上并不起作用。有什么方法可以实现我所尝试的吗?我曾考虑过创建一个模拟FakeUpdate
对象来实现Update
概念(这正是我希望编译器所做的),但感觉有点难看。
1条答案
按热度按时间u7up0aaq1#
下面的例子展示了一个实现你想要的东西的方法。我们简单地定义了
UpdateableDummy
结构体,并把它传递给我们的Handleable
概念。如果Handleable
概念可以从Handleable
类的模板中推导出传递给on_update
的类型,那就太好了。但对于 * 确实 * 是Handleable
但不是模板的类,这将失败(参见non_templateHandleableExample
)。因此,也许在未来的语言标准中,Handleable
概念中对on_update
的调用类型,可以从针对Handleable
lity检查的类中推导出来还应注意,
HandleableExample
声明中的template <Updateable T>
要求T
为Updateable
。