Skip to content

Instantly share code, notes, and snippets.

@pattersongp
Last active March 9, 2019 05:09
Show Gist options
  • Save pattersongp/f058819cef7f9af7291e4113c9e2c800 to your computer and use it in GitHub Desktop.
Save pattersongp/f058819cef7f9af7291e4113c9e2c800 to your computer and use it in GitHub Desktop.
/*
* Concepts Example:
*/
#include <iostream>
using namespace std;
template<typename T>
concept bool Addable = requires(T a, T b) {
{ a+b } -> T;
};
template<typename T>
concept bool Subtractable = requires(T a, T b) {
{ a-b } -> T;
};
template<typename T>
concept bool AddableAndSubtractable = Addable<T> && Subtractable<T>;
AddableAndSubtractable myFunction(AddableAndSubtractable a, AddableAndSubtractable b) {
return a + b;
}
Addable myAdd(Addable a, Addable b) {
return a + b;
}
class Add {
int x;
public:
Add(int x) : x(x) {}
Add operator+(Add a) { return *this; }
};
int main() {
cout << myFunction(1,2) << endl;
cout << "0x" << hex << myFunction(0xfff, 0xabc) << endl;
Add a(1);
Add b(1);
Add d = myAdd(a, b);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment