fork download
  1. #include <tuple>
  2.  
  3. #define DECL_INIT_ARRAY(TYPE, NAME, COUNT, ...) \
  4.   static_assert(COUNT == \
  5.   std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value, \
  6.   "Array " #NAME " should have exactly " #COUNT " initializers"); \
  7.   TYPE NAME[COUNT] = { __VA_ARGS__ }
  8.  
  9. DECL_INIT_ARRAY(const int, array3_3, 3, 1, 2, 3);
  10.  
  11. int main(void)
  12. {
  13. DECL_INIT_ARRAY(const int, array5_4, 5, 1, 2, 3, 4);
  14. DECL_INIT_ARRAY(const int, array5_6, 5, 1, 2, 3, 4, 5, 6);
  15. return 0;
  16. }
  17.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:13:3: error: static assertion failed: Array array5_4 should have exactly 5 initializers
prog.cpp:14:3: error: static assertion failed: Array array5_6 should have exactly 5 initializers
prog.cpp:14:3: error: too many initializers for ‘const int [5]’
prog.cpp:13:3: warning: unused variable ‘array5_4’ [-Wunused-variable]
prog.cpp:14:3: warning: unused variable ‘array5_6’ [-Wunused-variable]
stdout
Standard output is empty