Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Conjunction of concepts before auto?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading