File: cpp17_enable_if_t.i

package info (click to toggle)
swig 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 46,232 kB
  • sloc: cpp: 54,631; ansic: 29,122; java: 17,530; python: 12,505; cs: 10,369; ruby: 7,232; yacc: 6,477; makefile: 5,965; javascript: 5,520; sh: 5,415; perl: 4,187; php: 3,693; ml: 2,187; lisp: 2,056; tcl: 1,991; xml: 115
file content (118 lines) | stat: -rw-r--r-- 3,794 bytes parent folder | download | duplicates (2)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
%module cpp17_enable_if_t

// test use of enable_if_t but without full %template instantiation, that is no enable_if_t definition is parsed

%inline %{
#if defined(_MSC_VER) && _MSC_VER < 1920
#define or ||
#define and &&
#endif

#include <type_traits>
typedef int node_t;
typedef int position_t;

template <typename A, typename B, std::enable_if_t<std::is_integral_v<A>, bool> = true>
  void enableif1(const A a, const B b) {}

// tests non-type template parameters within () brackets - was causing an infinite loop, issue #2418
template <typename A, typename B, std::enable_if_t<(std::is_integral_v<A>), bool> = true>
  void enableif2(const A a, const B b) {}

template <typename A, typename B, std::enable_if_t<(std::is_integral_v<A> || std::is_same_v<A, node_t>), bool> = true>
  void enableif3(const A a, const B b) {}

template <typename A, typename B, std::enable_if_t<(std::is_integral_v<A> or std::is_same_v<A, node_t>) and (std::is_integral_v<B> or std::is_same_v<B, position_t>), bool> = true>
  void enableif4(const A a, const B b) {}

template <typename A, typename B, std::enable_if_t<(std::is_integral_v<A> and std::is_integral_v<B>), bool> = true>
  int enableif5(const A a, const B b) {
    return a + b;
  }

void tester() {
  enableif5<int, int>(10, 20);
  enableif5(10, 20);
}
%}

// non-type template parameters working well in SWIG, below is a simple workaround as the 3rd parameter is defaulted for enable_if_t (which is just SFINAE to give a nice C++ compiler error)
%template(enableif5) enableif5<int, int, true>; // workaround (overriding default)


%inline %{
// #1037 infinite loop
template <typename T, std::enable_if_t<sizeof(T) <= 4>>
void destId(T el) {}

template <typename T, std::enable_if_t<sizeof(T) >= 3>>
void destId(const T& el) {}
%}

%inline %{
// #961 no name for defaulted template parameter
template<typename T, typename = std::enable_if_t<std::is_enum<T>::value>>
void uuu() {}
template<typename T, typename E = std::enable_if_t<std::is_enum<T>::value>>
void uuuE() {}

template<typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
void vvv() {}
template<typename T, typename E = typename std::enable_if<std::is_floating_point<T>::value>::type>
void vvvE() {}

// More variations of enable_if and enable_if_t
template<typename T, typename std::enable_if<std::is_floating_point<T>::value>::type* = nullptr>
void www() {}

template<typename T, typename std::enable_if_t<std::is_enum<T>::value, int> = 0>
void xxx() {}

enum TestEnum { Enum1 = 1, Enum2 };
struct TestStruct {};

void tester2() {
  uuu<TestEnum>();
//  uuu<TestStruct>(); // compilation error
  uuuE<TestEnum>();
//  uuuE<TestStruct>(); // compilation error
  vvv<double>();
//  vvv<TestStruct>(); // compilation error
  vvvE<double>();
//  vvvE<TestStruct>(); // compilation error

  www<double>();
//  www<TestStruct>(); // compilation error
  xxx<TestEnum>();
//  xxx<TestStruct>(); // compilation error
}
%}

// Check fold expressions parse (#2868):
#define FOLD_EXPR_TEST(OP, FUNC) \
  template< \
    typename... Ts, \
    typename R = typename std::common_type_t<Ts...>, \
    std::enable_if_t< \
        (std::is_same_v<typename std::decay_t<Ts>,HalfInt> OP ...) \
        && (std::is_constructible_v<HalfInt, R> \
        || std::is_convertible_v<R, HalfInt>) \
      >* = nullptr \
  > \
  constexpr inline R FUNC(const Ts&... t) { return std::min(static_cast<R>(t)...); }
FOLD_EXPR_TEST(+, f1)
FOLD_EXPR_TEST(-, f2)
FOLD_EXPR_TEST(*, f3)
FOLD_EXPR_TEST(/, f4)
FOLD_EXPR_TEST(%, f5)
FOLD_EXPR_TEST(&, f6)
FOLD_EXPR_TEST(|, f7)
FOLD_EXPR_TEST(^, f8)
FOLD_EXPR_TEST(<<, f9)
FOLD_EXPR_TEST(>>, f10)
FOLD_EXPR_TEST(&&, f11)
FOLD_EXPR_TEST(||, f12)
FOLD_EXPR_TEST(==, f13)
FOLD_EXPR_TEST(!=, f14)
FOLD_EXPR_TEST(>=, f15)
FOLD_EXPR_TEST(<=, f16)