fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <map>
  5.  
  6.  
  7. // http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c
  8. std::vector<std::string> split(const std::string &text, char sep) {
  9. std::vector<std::string> tokens;
  10. size_t start = 0, end = 0;
  11. while ((end = text.find(sep, start)) != std::string::npos) {
  12. tokens.push_back(text.substr(start, end - start));
  13. start = end + 1;
  14. }
  15. tokens.push_back(text.substr(start));
  16. return tokens;
  17. }
  18.  
  19. std::vector<std::string> parse(const char* args)
  20. {
  21. return split(args, ',');
  22. }
  23.  
  24. std::vector<std::string> parse(const char* args);
  25.  
  26. template<typename T, typename ...Ts>
  27. std::map<T, std::string> make_map(const char* text, Ts... args)
  28. {
  29. std::vector<T> keys{args...};
  30. std::vector<std::string> vals = parse(text);
  31. auto k = keys.cbegin();
  32. auto v = vals.cbegin();
  33. std::map<T, std::string> r;
  34. for (; k != keys.cend(); k++, v++) {
  35. r.emplace(*k, *v);
  36. }
  37. return r;
  38.  
  39. }
  40.  
  41. #define ENUM(name, ...) \
  42. enum name \
  43. { \
  44.   __VA_ARGS__ \
  45. }; \
  46. static std::string to_string(const name v) { \
  47.   static std::map<name, std::string> m {make_map<name>(#__VA_ARGS__, __VA_ARGS__)};\
  48.   return m.at(v); \
  49. }
  50.  
  51.  
  52. ENUM(Color, Red,Green,Blue)
  53. namespace ns {
  54. ENUM(Alignment, Left,Right)
  55. }
  56.  
  57. class X
  58. {
  59. public:
  60. ENUM(Y,A=8,B,C)
  61. };
  62.  
  63.  
  64. int main(int , char**)
  65. {
  66. std::cout << to_string(Red) << to_string(Blue);
  67. std::cout << to_string(ns::Left) << to_string(ns::Right);
  68. std::cout << X::to_string(X::A) << X::to_string(X::B);
  69. return 0;
  70. }
  71.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In static member function 'static std::string X::to_string(X::Y)':
prog.cpp:60:13: error: lvalue required as left operand of assignment
     ENUM(Y,A=8,B,C)
             ^
prog.cpp:47:72: note: in definition of macro 'ENUM'
     static std::map<name, std::string> m {make_map<name>(#__VA_ARGS__, __VA_ARGS__)};\
                                                                        ^
prog.cpp:47:84: error: no matching function for call to 'std::map<X::Y, std::basic_string<char> >::map(<brace-enclosed initializer list>)'
     static std::map<name, std::string> m {make_map<name>(#__VA_ARGS__, __VA_ARGS__)};\
                                                                                    ^
prog.cpp:60:5: note: in expansion of macro 'ENUM'
     ENUM(Y,A=8,B,C)
     ^
In file included from /usr/include/c++/5/map:61:0,
                 from prog.cpp:4:
/usr/include/c++/5/bits/stl_map.h:273:9: note: candidate: template<class _InputIterator> std::map<_Key, _Tp, _Compare, _Alloc>::map(_InputIterator, _InputIterator, const _Compare&, const allocator_type&)
         map(_InputIterator __first, _InputIterator __last,
         ^
/usr/include/c++/5/bits/stl_map.h:273:9: note:   template argument deduction/substitution failed:
/usr/include/c++/5/bits/stl_map.h:256:9: note: candidate: template<class _InputIterator> std::map<_Key, _Tp, _Compare, _Alloc>::map(_InputIterator, _InputIterator)
         map(_InputIterator __first, _InputIterator __last)
         ^
/usr/include/c++/5/bits/stl_map.h:256:9: note:   template argument deduction/substitution failed:
/usr/include/c++/5/bits/stl_map.h:239:9: note: candidate: template<class _InputIterator> std::map<_Key, _Tp, _Compare, _Alloc>::map(_InputIterator, _InputIterator, const allocator_type&)
         map(_InputIterator __first, _InputIterator __last,
         ^
/usr/include/c++/5/bits/stl_map.h:239:9: note:   template argument deduction/substitution failed:
/usr/include/c++/5/bits/stl_map.h:233:7: note: candidate: std::map<_Key, _Tp, _Compare, _Alloc>::map(std::initializer_list<std::pair<const _Key, _Tp> >, const allocator_type&) [with _Key = X::Y; _Tp = std::basic_string<char>; _Compare = std::less<X::Y>; _Alloc = std::allocator<std::pair<const X::Y, std::basic_string<char> > >; std::map<_Key, _Tp, _Compare, _Alloc>::allocator_type = std::allocator<std::pair<const X::Y, std::basic_string<char> > >]
       map(initializer_list<value_type> __l, const allocator_type& __a)
       ^
/usr/include/c++/5/bits/stl_map.h:233:7: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/5/bits/stl_map.h:227:7: note: candidate: std::map<_Key, _Tp, _Compare, _Alloc>::map(std::map<_Key, _Tp, _Compare, _Alloc>&&, const allocator_type&) [with _Key = X::Y; _Tp = std::basic_string<char>; _Compare = std::less<X::Y>; _Alloc = std::allocator<std::pair<const X::Y, std::basic_string<char> > >; std::map<_Key, _Tp, _Compare, _Alloc>::allocator_type = std::allocator<std::pair<const X::Y, std::basic_string<char> > >]
       map(map&& __m, const allocator_type& __a)
       ^
/usr/include/c++/5/bits/stl_map.h:227:7: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/5/bits/stl_map.h:223:7: note: candidate: std::map<_Key, _Tp, _Compare, _Alloc>::map(const std::map<_Key, _Tp, _Compare, _Alloc>&, const allocator_type&) [with _Key = X::Y; _Tp = std::basic_string<char>; _Compare = std::less<X::Y>; _Alloc = std::allocator<std::pair<const X::Y, std::basic_string<char> > >; std::map<_Key, _Tp, _Compare, _Alloc>::allocator_type = std::allocator<std::pair<const X::Y, std::basic_string<char> > >]
       map(const map& __m, const allocator_type& __a)
       ^
/usr/include/c++/5/bits/stl_map.h:223:7: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/5/bits/stl_map.h:219:7: note: candidate: std::map<_Key, _Tp, _Compare, _Alloc>::map(const allocator_type&) [with _Key = X::Y; _Tp = std::basic_string<char>; _Compare = std::less<X::Y>; _Alloc = std::allocator<std::pair<const X::Y, std::basic_string<char> > >; std::map<_Key, _Tp, _Compare, _Alloc>::allocator_type = std::allocator<std::pair<const X::Y, std::basic_string<char> > >]
       map(const allocator_type& __a)
       ^
/usr/include/c++/5/bits/stl_map.h:219:7: note:   conversion of argument 1 would be ill-formed:
/usr/include/c++/5/bits/stl_map.h:211:7: note: candidate: std::map<_Key, _Tp, _Compare, _Alloc>::map(std::initializer_list<std::pair<const _Key, _Tp> >, const _Compare&, const allocator_type&) [with _Key = X::Y; _Tp = std::basic_string<char>; _Compare = std::less<X::Y>; _Alloc = std::allocator<std::pair<const X::Y, std::basic_string<char> > >; std::map<_Key, _Tp, _Compare, _Alloc>::allocator_type = std::allocator<std::pair<const X::Y, std::basic_string<char> > >]
       map(initializer_list<value_type> __l,
       ^
/usr/include/c++/5/bits/stl_map.h:211:7: note:   conversion of argument 1 would be ill-formed:
/usr/include/c++/5/bits/stl_map.h:196:7: note: candidate: std::map<_Key, _Tp, _Compare, _Alloc>::map(std::map<_Key, _Tp, _Compare, _Alloc>&&) [with _Key = X::Y; _Tp = std::basic_string<char>; _Compare = std::less<X::Y>; _Alloc = std::allocator<std::pair<const X::Y, std::basic_string<char> > >]
       map(map&& __x)
       ^
/usr/include/c++/5/bits/stl_map.h:196:7: note:   conversion of argument 1 would be ill-formed:
/usr/include/c++/5/bits/stl_map.h:185:7: note: candidate: std::map<_Key, _Tp, _Compare, _Alloc>::map(const std::map<_Key, _Tp, _Compare, _Alloc>&) [with _Key = X::Y; _Tp = std::basic_string<char>; _Compare = std::less<X::Y>; _Alloc = std::allocator<std::pair<const X::Y, std::basic_string<char> > >]
       map(const map& __x)
       ^
/usr/include/c++/5/bits/stl_map.h:185:7: note:   conversion of argument 1 would be ill-formed:
/usr/include/c++/5/bits/stl_map.h:174:7: note: candidate: std::map<_Key, _Tp, _Compare, _Alloc>::map(const _Compare&, const allocator_type&) [with _Key = X::Y; _Tp = std::basic_string<char>; _Compare = std::less<X::Y>; _Alloc = std::allocator<std::pair<const X::Y, std::basic_string<char> > >; std::map<_Key, _Tp, _Compare, _Alloc>::allocator_type = std::allocator<std::pair<const X::Y, std::basic_string<char> > >]
       map(const _Compare& __comp,
       ^
/usr/include/c++/5/bits/stl_map.h:174:7: note:   conversion of argument 1 would be ill-formed:
/usr/include/c++/5/bits/stl_map.h:162:7: note: candidate: std::map<_Key, _Tp, _Compare, _Alloc>::map() [with _Key = X::Y; _Tp = std::basic_string<char>; _Compare = std::less<X::Y>; _Alloc = std::allocator<std::pair<const X::Y, std::basic_string<char> > >]
       map()
       ^
/usr/include/c++/5/bits/stl_map.h:162:7: note:   candidate expects 0 arguments, 1 provided
stdout
Standard output is empty