cxx_variadic_templates.cpp 1.3 KB

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