Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Concepts & constraints do what SFINAE does
#include <iostream>
/* are concept constraint violations like SFINAE "errors", or like, uh...
* ... the other kind?
* they're like SFINAE errors! constraint violations take function templates
* out of consideration for overloading, but are not errors.
*/
template<class T>
concept fugastic = requires {
typename T::fug;
};
struct inty {
int x;
using fug = const char *;
operator int() { return x; }
};
struct inty_not_fuggy {
int x;
operator int() { return x; }
};
double do_it(double x) { return x * 3.7; }
int do_it(int x) { return x * 5; }
template<fugastic T>
auto do_it(T x) { return x - 7; }
int main()
{
std::cout << do_it(5.0) << '\n';
std::cout << do_it(5) << '\n';
std::cout << do_it(inty { 5 }) << '\n';
std::cout << do_it(inty_not_fuggy { 5 }) << '\n';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.