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

Why is concept and class having same name disallowed

Maybe there is a very simple reason but I so far couldn’t figure out. Why does the standard not allow it? Concepts can’t be used for scope resolution, and one can’t use concepts as function parameters without auto. So I don’t see at what places would it be impossible for the compiler to tell clearly whether the programmer wanted to use the concept or the type, even though they have the same name.

SomeConcept::Whatever                 // not possible
void someFunc( SomeConcept auto& x )  // auto required
template<SomeConcept T>               // concept instead of "typename"

"Easier to parse" I don’t think is enough reason.

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

>Solution :

There is at least one case where the compiler would not be able to distinguish the meaning: in a template parameter, the identifier SomeConcept could be a concept, or a non-type template parameter of class type.

e.g., the following is valid:

struct SomeConcept {};
template<SomeConcept T> void f() {}

// ... later used like
f<SomeConcept{}>();
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