fork(1) download
  1. #include <type_traits>
  2. #include <array>
  3.  
  4. template <bool...> struct bools {};
  5.  
  6. template <bool... bs>
  7. using all_of = ::std::is_same<bools<true, bs...>, bools<bs..., true>>;
  8.  
  9. template<typename... Ts>
  10. using contains_only_array_of_3_int =
  11. all_of<::std::is_same<::std::array<int, 3>, Ts>::value...>;
  12.  
  13. template <std::size_t V>
  14. class Test
  15. {
  16. public:
  17. Test() {}
  18.  
  19. template <typename... TArgs>
  20. Test(TArgs&& ... args)
  21. {
  22. static_assert(sizeof...(TArgs) == V, "arguments count mismatch");
  23. static_assert(contains_only_array_of_3_int<::std::decay_t<TArgs>...>::value,
  24. "arguments type mismatch");
  25. }
  26. };
  27.  
  28. int main()
  29. {
  30. ::std::array<int, 3> a{ 1, 2, 3 };
  31. Test<1> t
  32. {
  33. ::std::array<int, 3>{ 1, 2, 3 }
  34. };
  35. Test<2> t2
  36. {
  37. ::std::array<int, 3>{ 1, 2, 3 }
  38. , ::std::array<int, 3>{ 4, 5, 6 }
  39. };
  40. Test<3> t3
  41. {
  42. a
  43. , ::std::array<int, 3>{ 4, 5, 6 }
  44. , ::std::array<int, 3>{ 7, 8, 9 }
  45. };
  46. Test<3> t3_args_count_fail
  47. {
  48. ::std::array<int, 3>{ 1, 2, 3 }
  49. , ::std::array<int, 3>{ 4, 5, 6 }
  50. };
  51. Test<3> t3_args_tupe_fail
  52. {
  53. ::std::array<int, 3>{ 1, 2, 3 }
  54. , ::std::array<int, 2>{ 4, 5 }
  55. , ::std::array<int, 3>{ 7, 8, 9 }
  56. };
  57. return(0);
  58. }
  59.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of ‘Test<V>::Test(TArgs&& ...) [with TArgs = {std::array<int, 3ul>, std::array<int, 3ul>}; long unsigned int V = 3ul]’:
prog.cpp:50:2:   required from here
prog.cpp:22:3: error: static assertion failed: arguments count mismatch
   static_assert(sizeof...(TArgs) == V, "arguments count mismatch");
   ^~~~~~~~~~~~~
prog.cpp: In instantiation of ‘Test<V>::Test(TArgs&& ...) [with TArgs = {std::array<int, 3ul>, std::array<int, 2ul>, std::array<int, 3ul>}; long unsigned int V = 3ul]’:
prog.cpp:56:2:   required from here
prog.cpp:23:3: error: static assertion failed: arguments type mismatch
   static_assert(contains_only_array_of_3_int<::std::decay_t<TArgs>...>::value,
   ^~~~~~~~~~~~~
stdout
Standard output is empty