File: cxx_variadic_templates.cpp

package info (click to toggle)
cmake 3.25.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 121,376 kB
  • sloc: ansic: 361,053; cpp: 250,806; sh: 3,828; yacc: 3,243; python: 2,707; lex: 1,328; lisp: 382; asm: 371; f90: 314; java: 266; perl: 217; objc: 212; xml: 202; cs: 200; fortran: 131; makefile: 99; javascript: 83; pascal: 63; tcl: 55; php: 25; ruby: 22
file content (72 lines) | stat: -rw-r--r-- 1,467 bytes parent folder | download | duplicates (7)
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
#if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) < 407)
#  define OLD_GNU
#endif

#ifdef OLD_GNU
template <int... Is>
struct Interface;
#endif

template <int I, int... Is>
struct Interface
#ifdef OLD_GNU
  <I, Is...>
#endif
{
  static int accumulate() { return I + Interface<Is...>::accumulate(); }
};

template <int I>
struct Interface<I>
{
  static int accumulate() { return I; }
};

// Note: split this into a separate test if a
// cxx_variadic_template_template_parameters feature is added.

template <typename T>
struct eval
{
  enum
  {
    Matched = 0
  };
};

template <template <typename...> class T, typename... U>
struct eval<T<U...>>
{
  enum
  {
    Matched = 1
  };
};

template <typename...>
struct A
{
};
template <typename T>
struct B
{
};
template <typename T, typename U>
struct C
{
};
template <typename T, typename U, typename...>
struct D
{
};

// Note: This test assumes that a compiler supporting this feature
// supports static_assert. Add a workaround if that does not hold.
static_assert(eval<A<>>::Matched, "A Matches");
static_assert(eval<A<int>>::Matched, "A Matches");
static_assert(eval<A<int, char>>::Matched, "A Matches");
static_assert(eval<B<int>>::Matched, "B Matches");
static_assert(eval<C<int, char>>::Matched, "C Matches");
static_assert(eval<D<int, char>>::Matched, "D Matches");
static_assert(eval<D<int, char, bool>>::Matched, "D Matches");
static_assert(eval<D<int, char, bool, double>>::Matched, "D Matches");