fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <type_traits>
  4. #include <memory>
  5.  
  6. class Light{};
  7. class PointLight: public Light{};
  8. class DirectionalLight: public Light{};
  9.  
  10. std::vector< std::unique_ptr<Light> > lights;
  11.  
  12. template<typename T>
  13. void CreateLight()
  14. {
  15. static_assert(std::is_base_of<Light, T>::value, "Type must of descendant of type Light. ");
  16. // store to vector directly
  17. lights.emplace_back( std::make_unique<T>(/*args*/));
  18. }
  19.  
  20. int main()
  21. {
  22. CreateLight<PointLight>();
  23.  
  24. //Either directly
  25. auto iter = lights.rbegin();
  26. if(*iter) std::cout << "Done\n";
  27.  
  28.  
  29. return 0;
  30. }
Success #stdin #stdout 0s 4520KB
stdin
Standard input is empty
stdout
Done