cxx_variadic_templates.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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() { return I + Interface<Is...>::accumulate(); }
  15. };
  16. template <int I>
  17. struct Interface<I>
  18. {
  19. static int accumulate() { return I; }
  20. };
  21. // Note: split this into a separate test if a
  22. // cxx_variadic_template_template_parameters feature is added.
  23. template <typename T>
  24. struct eval
  25. {
  26. enum
  27. {
  28. Matched = 0
  29. };
  30. };
  31. template <template <typename...> class T, typename... U>
  32. struct eval<T<U...>>
  33. {
  34. enum
  35. {
  36. Matched = 1
  37. };
  38. };
  39. template <typename...>
  40. struct A
  41. {
  42. };
  43. template <typename T>
  44. struct B
  45. {
  46. };
  47. template <typename T, typename U>
  48. struct C
  49. {
  50. };
  51. template <typename T, typename U, typename...>
  52. struct D
  53. {
  54. };
  55. // Note: This test assumes that a compiler supporting this feature
  56. // supports static_assert. Add a workaround if that does not hold.
  57. static_assert(eval<A<>>::Matched, "A Matches");
  58. static_assert(eval<A<int>>::Matched, "A Matches");
  59. static_assert(eval<A<int, char>>::Matched, "A Matches");
  60. static_assert(eval<B<int>>::Matched, "B Matches");
  61. static_assert(eval<C<int, char>>::Matched, "C Matches");
  62. static_assert(eval<D<int, char>>::Matched, "D Matches");
  63. static_assert(eval<D<int, char, bool>>::Matched, "D Matches");
  64. static_assert(eval<D<int, char, bool, double>>::Matched, "D Matches");