Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a concept as a template parameter?

I am guessing this is not possible, but I am really hoping it is. An example:

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>() );

Is there a way to do something like this?

If not, is there a hack to achieve the same functionality?

Ultimately I'd like to use compound concepts as placeholder constraints.

like image 314
evenex_code Avatar asked Sep 10 '25 10:09

evenex_code


1 Answers

Concepts can really only be used in limited ways (as of this writing). As a consequence, it is not possible to achieve what you want.

A workaround would be to associate to each concept a particular (meta)function in order to enable higher-order use:

// 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);
like image 126
Luc Danton Avatar answered Sep 13 '25 00:09

Luc Danton