Home > Software engineering >  Conjunction of concepts before auto?
Conjunction of concepts before auto?

Time:12-17

I'm learning C 20 concepts. Is there a way to do a conjunction of concepts in-place before an auto? For example, If I have a MutableGraph<G> concept and a VertexListGraph<G> concept, I could define

template <typename G>
concept MutableVertexListGraph = MutableGraph<G> && VertexListGraph<G>;

and do

MutableVertexListGraph auto g = ...;

but it's annoying to name the concept when it's just that I want it to model both concepts. It would be nice if I could do

MutableGraph && VertexListGraph auto g = ...; // && not allowed here.

or even something like

template <typename T, concept... Concepts> // <- "concept" not allowed here.
concept ModelsAll = (Concepts<T> && ...);
...
ModelsAll<MutableGraph, VertexListGraph> auto g = ...;

I can of course do

MutableGraph auto g = ...;
requires { MutableGraph<decltype(g)>; };

which lacks symmetry, or

auto g = ...;
requires { MutableGraph<decltype(g)>; MutableGraph<decltype(g)>; };

which lacks the concept on the declaration line.

CodePudding user response:

No, there is no way to combine concepts when applying constraints to a deduced variable definition's type.

ConceptName auto variable = ...; is fairly readable. The ConceptName is probably pretty self-explanatory, and it leaves plenty of room for the variable definition.

Concept1 && Concept2 && Concept3 auto variable = ...; is a lot less readable. At that point, the sequence of concepts crowds out the actually important parts of the statement: the variable declaration. As such, splitting it into multiple statements makes a lot more sense:

auto variable = ...;
static_assert(Concept1<decltype(variable)> && Concept2<decltype(variable)> && Concept3<decltype(variable)>);

This involves a very unfortunate amount of decltype usage (which an additional using statement could avoid), but it does separate the concerns, making the variable declaration itself more readable.

  • Related