summaryrefslogtreecommitdiff
path: root/Examples/test-suite/cpp11_template_typedefs.i
blob: 5f1f2e6f9a291fa132e7cd6f6924bee36ebf2883 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* This testcase checks whether SWIG correctly handles alias templates. */
%module cpp11_template_typedefs

%inline %{

template<typename T>
using ptr_t = T*;

namespace ns {

template<typename T1, typename T2, int N>
class SomeType {
public:
  using type1_t = T1;
  using type2_t = T2;
  T1 a;
  T2 b;
  constexpr int get_n() const { return N; }
};

// Specialization for T1=const char*, T2=bool
template<int N>
class SomeType<const char*, bool, N> {
public:
  using type1_t = const char*;
  using type2_t = bool;
  type1_t a;
  type2_t b;
  constexpr int get_n() const { return 3 * N; }
};

// alias templates
template<typename T2>
using TypedefName = SomeType<const char*, T2, 5>;
template<typename T2>
using TypedefNamePtr = ptr_t<SomeType<const char*, T2, 4>>;

// alias template that returns T2 for a SomeType<T1,T2,N> class
template<typename T>
using T2_of = typename T::type2_t;

T2_of<TypedefName<int>> get_SomeType_b(const SomeType<const char*, int, 5>& x) { return x.b; }

template<typename T>
T2_of<TypedefName<T>> get_SomeType_b2(const TypedefName<T>& x) { return x.b; }

} // namespace ns

ns::TypedefName<int> create_TypedefName() { return { "hello", 10}; }
ns::TypedefName<bool> create_TypedefNameBool() { return { "hello", true}; }
ns::TypedefNamePtr<int> identity(ns::TypedefNamePtr<int> a = nullptr) { return a; }

typedef double Val;
template<typename T> struct ListBucket {
};
namespace Alloc {
  template<typename T> struct rebind {
    using other = int;
  };
}

using BucketAllocator1 = typename Alloc::template rebind<ListBucket<Val>>::other;
using BucketAllocator2 = typename Alloc::template rebind<::template ListBucket<double>>::other;

BucketAllocator1 get_bucket_allocator1() { return 1; }
BucketAllocator2 get_bucket_allocator2() { return 2; }
%}

%immutable ns::SomeType::a;

// %template() directives

%template(SomeTypeInt5) ns::SomeType<const char*, int, 5>;
%template(SomeTypeInt4) ns::SomeType<const char*, int, 4>;
%template(SomeTypeBool5) ns::SomeType<const char*, bool, 5>;

%template(ListBucketDouble) ListBucket<Val>;
%template(RebindListBucketDouble) Alloc::rebind<ListBucket<Val>>;

%template() ptr_t<ns::SomeType<const char*, int, 4>>;
%template() ns::TypedefName<int>;
%template() ns::TypedefName<bool>;
%template() ns::TypedefNamePtr<int>;
%template() ns::T2_of<ns::TypedefName<int>>;

%template(get_SomeType_b2) ns::get_SomeType_b2<int>;