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

Concept subsumption working for functions, but not for structs

Apologies for potentially wrong title, that is my best guess what is happening.

I was learning some basic concepts and tried this:

#include <concepts>
#include <iostream>
#include <memory>

template<typename T>
concept eight = sizeof(T) == 8;

template<typename T>
concept basic = std::is_trivial_v<T>;

template<typename T>
requires eight<T>
constexpr void ff(){
    std::cout << "eight" << std::endl;
}

template<typename T>
requires eight<T> && basic<T>
constexpr void ff(){
    std::cout << "eight and basic" << std::endl;
}

template<typename T>
requires eight<T>
struct ffs{
};

template<typename T>
requires eight<T> && basic<T>
struct ffs{
};

What is insane to me is that I get error for struct when same stuff works for functions.

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

:29:10: error: requires clause differs in template
redeclaration requires eight && basic
^ :24:10: note: previous template declaration is here requires eight

It could be that I am just UB+NDRing in both cases but compiler does not mind in first case(not that when I remove structs from code my code seems to run as expected, including distinguishing properly what to invoke based on concepts), but that seems unlikely.

P.S. if somebody wonders why I just don’t use requires instead of trivial concepts here is the answer.

>Solution :

Overloading template classes wasn’t allowed before concepts, and it still not allowed even with concepts. Use partial specialization:

template <typename T>
requires eight<T>
struct ffs {};

template <typename T>
requires basic<T>
struct ffs<T> {};

Or with the terse syntax:

template <eight T>
struct ffs {};

template <basic T>
struct ffs<T> {};

Note that in both cases, the constraints on the primary template automatically apply to all specializations.

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