fork download
  1. #include <iostream>
  2. #include <type_traits>
  3. #include <list>
  4. #include <vector>
  5. #include <memory>
  6.  
  7. struct Base
  8. {
  9. virtual void doSomething(int) = 0;
  10. };
  11.  
  12. struct DerivedOne: public Base
  13. {
  14. void doSomething(int)
  15. {
  16. std::cout << __PRETTY_FUNCTION__ << '\n';
  17. }
  18. };
  19.  
  20. struct DerivedTwo: public Base
  21. {
  22. void doSomething(int)
  23. {
  24. std::cout << __PRETTY_FUNCTION__ << '\n';
  25. }
  26. };
  27.  
  28. template<template<class, class...> class C, class... Args>
  29. void myFunc(C<std::shared_ptr<Base>, Args...>& objs, int y)
  30. {
  31. for(auto it = std::begin(objs); it != std::end(objs); ++it)
  32. (*it)->doSomething(y);
  33. }
  34.  
  35. int main()
  36. {
  37. // list of shared pointers to Base
  38. std::list<std::shared_ptr<Base>> objs;
  39.  
  40. objs.push_back(std::make_shared<DerivedOne>());
  41. objs.push_back(std::make_shared<DerivedTwo>());
  42. myFunc(objs, 42);
  43.  
  44. // vector of shared pointers to base
  45. std::vector<std::shared_ptr<Base>> vec(objs.rbegin(), objs.rend());;
  46. myFunc(vec,42);
  47. }
  48.  
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
virtual void DerivedOne::doSomething(int)
virtual void DerivedTwo::doSomething(int)
virtual void DerivedTwo::doSomething(int)
virtual void DerivedOne::doSomething(int)