fork download
  1. #include <iostream>
  2. #include <type_traits>
  3. #include <vector>
  4.  
  5.  
  6. struct MTop {};
  7.  
  8. template <class Child>
  9. class MainObject : public MTop
  10. {
  11. public:
  12. Child * Add(char const* param);
  13. void SetParam(char const * param) {/*some code*/}
  14.  
  15. private:
  16. std::vector<Child*> Items;
  17. };
  18.  
  19. template<typename T>
  20. typename std::enable_if<!std::is_base_of<MTop, T>::value>::type
  21. set_param_if_needed(T *, char const*) {
  22. std::clog << "empty" << std::endl;
  23. }
  24.  
  25. template<typename T>
  26. typename std::enable_if<std::is_base_of<MTop, T>::value>::type
  27. set_param_if_needed(T * const object, char const* const param) {
  28. std::clog << "not empty" << std::endl;
  29. object->SetParam(param);
  30. }
  31.  
  32. template <class Child>
  33. Child * MainObject<Child>::Add(char const* param){
  34. Child* Temp=new Child;
  35. set_param_if_needed(Temp, param);
  36. Items.push_back(Temp);
  37. return Temp;
  38. }
  39.  
  40. class SomeData:public MainObject<int>
  41. {
  42. };
  43. class SomeOtherData:public MainObject<SomeData>
  44. {
  45. };
  46.  
  47.  
  48. int main() {
  49. MainObject<int> intObject;
  50. intObject.Add("param");
  51.  
  52. MainObject<SomeData> mainObject;
  53. mainObject.Add("param");
  54.  
  55. MainObject<SomeOtherData> otherObject;
  56. otherObject.Add("param");
  57. }
Success #stdin #stdout #stderr 0s 3424KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
empty
not empty
not empty