-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddable.cxx
More file actions
30 lines (23 loc) · 863 Bytes
/
addable.cxx
File metadata and controls
30 lines (23 loc) · 863 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <concepts> // concept
#include <print> // std::print
#include <string> // std::string
template <class T, class U = T>
concept addable = requires(T a, U b) {
a + b;
b + a; // if we do not include this, we are not guaranteed that addition on T is commutative
};
addable auto add_addables(addable auto a, addable auto b) {
return a + b;
}
struct NotAddable {
int x = 0;
};
int main() {
std::print("{}\n", add_addables(5, 4));
std::print("{}\n", add_addables(5.4, 4));
std::print("{}\n", add_addables(std::string("Hello "), std::string("world")));
std::print("{}\n", add_addables(std::string("Hello "), 'W'));
std::print("{}\n", add_addables<std::string, char>(std::string("Hello "), 'W'));
// add_addables(NotAddable{1}, NotAddable{5}); // the required expression ‘(a + b)’ is invalid
return 0;
}