如何将概念用作模板参数

How can I use a concept as a template parameter?

本文关键字:参数      更新时间:2023-10-16

我猜这是不可能的,但我真的希望是。举个例子:

template<class T>
concept bool Dereferencable() { return requires(T t){ *t }; }
template<class T>
concept bool Incrementable() { return requires(T t){ ++t }; }
template<class T, ??? X, ??? Y>
concept bool And() { return X<T>() && Y<T>(); }
static_assert( And<int*, Dereferencable, Incrementable>() );

有没有办法做这样的事情?

如果没有,是否有黑客可以实现相同的功能?

最终,我想使用复合概念作为占位符约束。

概念实际上

只能以有限的方式使用(截至撰写本文时(。因此,不可能实现您想要的。

解决方法是将特定(元(函数与每个概念相关联,以便启用高阶使用:

// this is just one way of doing it...
template<typename Arg>
struct is_dereferencable_f {
    static constexpr bool value = Dereferencable<Arg>;
};
// ...via template template parameters
template<typename Arg, template<typename> typename... Conjuncts>
concept bool SatisfiesAll = (... && Conjuncts<Arg>::value);