fork download
  1. #include <iostream>
  2.  
  3. #define VA_NARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N
  4. #define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 5, 4, 3, 2, 1)
  5.  
  6. template <int... Args>
  7. struct arg_counter {
  8. enum { count = sizeof...(Args) };
  9. };
  10.  
  11. #define INC_COUNTER1 arg_counter<-1>::count
  12. #define INC_COUNTER2 arg_counter<-1, __COUNTER__>::count
  13. #define INC_COUNTER3 arg_counter<-1, __COUNTER__, __COUNTER__>::count
  14. #define INC_COUNTER4 arg_counter<-1, __COUNTER__, __COUNTER__, __COUNTER__>::count
  15. #define INC_COUNTER5 arg_counter<-1, __COUNTER__, __COUNTER__, __COUNTER__, __COUNTER__>::count
  16.  
  17. #define INC_COUNTER_IMPL2(count, ...) INC_COUNTER ## count
  18. #define INC_COUNTER_IMPL(count, ...) INC_COUNTER_IMPL2(count, __VA_ARGS__)
  19. #define INC_COUNTER(...) INC_COUNTER_IMPL(VA_NARGS(__VA_ARGS__), __VA_ARGS__)
  20.  
  21. // removed: __asm__( ".readonly__" #location "__" #name)
  22. #define CreateReadOnlyBlockImpl(name, location, size, ...) \
  23.   template<> \
  24.   const unsigned int ReadOnlyBlock<location, size>::Data[] \
  25.   = { __VA_ARGS__ }; \
  26.   ReadOnlyBlock<location, size> name;
  27.  
  28.  
  29. #define CreateReadOnlyBlock(name, ...) \
  30.   CreateReadOnlyBlockImpl(name, __COUNTER__, INC_COUNTER(__VA_ARGS__), __VA_ARGS__);
  31.  
  32. template<int Location, int Size> struct ReadOnlyBlock
  33. {
  34. static const unsigned int Data[Size];
  35. int loc () const { return Location; }
  36. int size() const { return Size; }
  37. };
  38.  
  39. CreateReadOnlyBlock(readOnlyArray1, 0, 1, 2, 3);
  40. CreateReadOnlyBlock(readOnlyArray2, 4, 5, 6, 7);
  41. CreateReadOnlyBlock(readOnlyArray3, 9);
  42. CreateReadOnlyBlock(readOnlyArray4, 1, 2, 3, 4, 5);
  43.  
  44. int main()
  45. {
  46. std::cout << "@" << readOnlyArray1.loc() << ": " << readOnlyArray1.size() << '\n';
  47. std::cout << "@" << readOnlyArray2.loc() << ": " << readOnlyArray2.size() << '\n';
  48. std::cout << "@" << readOnlyArray3.loc() << ": " << readOnlyArray3.size() << '\n';
  49. std::cout << "@" << readOnlyArray4.loc() << ": " << readOnlyArray4.size() << '\n';
  50. }
Success #stdin #stdout 0s 2852KB
stdin
Standard input is empty
stdout
@0: 4
@4: 4
@8: 1
@9: 5