fork download
  1. // types.h
  2. enum Types
  3. {
  4. kType1,
  5. kType2
  6. // ...
  7. };
  8.  
  9. struct Base {};
  10.  
  11. // d1.h
  12. class D1 : public Base
  13. {
  14. public:
  15. static const int type = kType1;
  16. // ...
  17. };
  18.  
  19. // d2.h
  20. class D2 : public Base
  21. {
  22. public:
  23. static const int type = kType2;
  24. // ...
  25. };
  26.  
  27. struct MyObject {};
  28.  
  29. #include <map>
  30. // other.cpp
  31. class Foo
  32. {
  33. std::multimap<int, MyObject> mm_;
  34.  
  35. template<typename T>
  36. void doSomething(MyObject obj)
  37. {
  38. mm_.insert(std::multimap<int, MyObject>::value_type(T::type, obj));
  39. }
  40.  
  41. public:
  42. void bar() {
  43. MyObject obj;
  44. doSomething<D1>(obj);
  45. doSomething<D2>(obj);
  46. }
  47. };
  48.  
  49. int main() {
  50. Foo f;
  51. f.bar();
  52. }
  53.  
Success #stdin #stdout 0s 2980KB
stdin
Standard input is empty
stdout
Standard output is empty