Skip to content

Instantly share code, notes, and snippets.

@charlesxsh
Last active February 17, 2019 10:58
Show Gist options
  • Save charlesxsh/9e4f084d9902b75195458f96b38b6ca5 to your computer and use it in GitHub Desktop.
Save charlesxsh/9e4f084d9902b75195458f96b38b6ca5 to your computer and use it in GitHub Desktop.
struct Dog {};
struct Husky : public Dog {};
struct Poodle :public Dog {};
template<class T>
struct is_husky {
static constexpr bool result = false;
};
template<>
struct is_husky<Husky> {
static constexpr bool result = true;
};
template<typename T>
concept bool ExceptHusky = !is_husky<T>::result; // If T is husky, this concept returns false
template<ExceptHusky T>
void DogCanBeSmartExceptHusky(const T&& dog) {};
/*
template<class T> requires ExceptHusky<T>
void DogCanBeSmartExceptHusky(const T&& dog) {};
also works. This is for multiple require expressions, they can use ||, &, ! such operators to join
*/
int main()
{
DogCanBeSmartExceptHusky(Poodle{});
DogCanBeSmartExceptHusky(Husky{});
// Error message here:
// constraints not satisfied
//'!(is_husky<Husky>::result)' evaluated to false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment