How would I provide a template specialisation for the Derived class in this simple case, assuming the Base cannot be changed - in my real code, Base is a library that I cannot change.
#include <iostream> #include <memory> class Base { public: virtual void foo() { std::cout << "In base\n"; } }; class Derived: public Base { public: virtual void foo() { std::cout << "In derived\n"; } }; template<typename T> void wibble(T &&baz) { std::cout << "In wibble for default\n"; baz->foo(); } // How do I provide a specialisation here? //template<typename what_goes_here> //void wibble(what_goes_here &&baz) //{ // std::cout << "In wibble for derived\n"; // baz->foo(); //} int main() { std::shared_ptr<Base> bar = std::make_shared<Derived>(); bar->foo(); wibble(bar); return 0; } I want to be able to use a separate template when the actual type contained within the shared pointer is Derived.
shared_ptr<Base>. The fact that it points towards aDerivedis technically a run-time information. So you cannot at compile-time disambiguate the two. You can call run-time dynamic casts to figure out what is what but that's it.shared_ptr. If thewibblecannot be modified then you cannot do much.