-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.cpp
64 lines (49 loc) · 1.62 KB
/
test.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <array>
#include <vector>
#include <boost/variant.hpp>
// Vector class
template < typename Scalar, size_t n >
class Vector
{
std::array<Scalar, n> d;
public:
Vector() : d() {}
template <typename T>
explicit Vector(T const &v) : d()
{
assert(std::distance(std::begin(v), std::end(v)) == n);
std::copy(std::begin(v), std::end(v), d.begin());
}
Scalar &operator[](size_t i) { return d[i]; }
Scalar const &operator[](size_t i) const { return d[i]; }
};
// type checking using SFINAE
template < typename T >
struct is_vector : std::false_type {};
template < typename Scalar, size_t n >
struct is_vector < Vector < Scalar, n > > : std::true_type {};
typedef Vector<double, 2> Vector2d;
typedef Vector<double, 3> Vector3d;
typedef Vector<double, 4> Vector4d;
// Variant
typedef boost::variant<
// other types
std::vector<double>
> Variant;
template < typename T >
typename std::enable_if<!is_vector<T>::value, T>::type
get_value(Variant const& v) { return boost::get<T>(v); }
template < typename T >
typename std::enable_if<is_vector<T>::value, T>::type
get_value(Variant const& v) { return T(boost::get<std::vector<double>>(v)); }
// Testing the implementation
int main()
{
Vector2d v2 = get_value<Vector2d>(std::vector<double>{1, 2});
Vector3d v3 = get_value<Vector3d>(std::vector<double>{1, 2, 3}); // fails
Vector4d v4 = get_value<Vector4d>(std::vector<double>{1, 2, 3, 4}); // fails
std::cout << v2[0] << " " << v2[1] << "\n";
std::cout << v3[0] << " " << v3[1] << " " << v3[2] << "\n";
std::cout << v4[0] << " " << v4[1] << " " << v4[2] << " " << v4[3] << "\n";
}