1
0

cxx_variadic_templates.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) < 407)
  2. #define OLD_GNU
  3. #endif
  4. #ifdef OLD_GNU
  5. template<int... Is>
  6. struct Interface;
  7. #endif
  8. template<int I, int... Is>
  9. struct Interface
  10. #ifdef OLD_GNU
  11. <I, Is...>
  12. #endif
  13. {
  14. static int accumulate()
  15. {
  16. return I + Interface<Is...>::accumulate();
  17. }
  18. };
  19. template<int I>
  20. struct Interface<I>
  21. {
  22. static int accumulate()
  23. {
  24. return I;
  25. }
  26. };
  27. // Note: split this into a separate test if a
  28. // cxx_variadic_template_template_parameters feature is added.
  29. template<typename T>
  30. struct eval {
  31. enum {
  32. Matched = 0
  33. };
  34. };
  35. template<template<typename...> class T, typename... U>
  36. struct eval<T<U...> > {
  37. enum {
  38. Matched = 1
  39. };
  40. };
  41. template<typename...>
  42. struct A {
  43. };
  44. template<typename T>
  45. struct B {
  46. };
  47. template<typename T, typename U>
  48. struct C {
  49. };
  50. template<typename T, typename U, typename...>
  51. struct D {
  52. };
  53. // Note: This test assumes that a compiler supporting this feature
  54. // supports static_assert. Add a workaround if that does not hold.
  55. static_assert(eval<A<> >::Matched, "A Matches");
  56. static_assert(eval<A<int> >::Matched, "A Matches");
  57. static_assert(eval<A<int, char> >::Matched, "A Matches");
  58. static_assert(eval<B<int> >::Matched, "B Matches");
  59. static_assert(eval<C<int, char> >::Matched, "C Matches");
  60. static_assert(eval<D<int, char> >::Matched, "D Matches");
  61. static_assert(eval<D<int, char, bool> >::Matched, "D Matches");
  62. static_assert(eval<D<int, char, bool, double> >::Matched, "D Matches");