Last active
March 9, 2019 05:09
-
-
Save pattersongp/f058819cef7f9af7291e4113c9e2c800 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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