-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconstrained.cpp
35 lines (24 loc) · 896 Bytes
/
constrained.cpp
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
31
32
33
34
35
#include <iostream>
#define TC_COMPAT
#include "../tc_alt.hpp"
// an example of constrained instantiation
template<class> struct Foo;
TC_DEF(Good, class T, {});
template<class T>
TC_INSTANCE(Good, Foo<T>, {
TC_REQUIRE(Good<T>); // constraint on an instance definition
// DIFFERENCE
// A possible alternative: Good<T> GoodT;
// note that in contrast to the tc.hpp
// one defines a variable instead of a type alias
});
template<> TC_INSTANCE(Good, int, {});
int main() {
// constraints on a function definition
TC_REQUIRE(Good<Foo<int>>); // satisfied
TC_REQUIRE(Good<Foo<Foo<int>>>); // satisfied
// TC_REQUIRE(Good<Foo<double>>); // won't compile
typedef Good<Foo<double>> ThisCompiles;
// ThisCompiles NOT; // an explicit variable can be used
// ThisCompiles(); // or the form which doesn't use a gratuitous identifier
}