1

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.

2
  • 4
    In the code you have an instance of shared_ptr<Base>. The fact that it points towards a Derived is 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. Commented Dec 22, 2021 at 7:56
  • You cannot specialized a template based on the runtime type of shared_ptr. If the wibble cannot be modified then you cannot do much. Commented Dec 22, 2021 at 7:57

1 Answer 1

1

The proper tool here is to use dynamic_cast<Derived*>. This is runtime information and as such must be queried at runtime.

Something like

void wibble(std::shared_ptr<Base*> baz){ if(auto derived =dynamic_cast<Derived*>(baz.get())){ // user derived pointer. } } 

should allow you achieve something similar to what you want. If your keyboard is missing *, you can also (thanks Eljay)

if(auto derived = std::dynamic_pointer_cast<Derived>(baz)){ // use derived } 

This is just one more case where templates and polymorphism doesn't really play nice with each other, mainly because of the compile time/runtime dichotomy.

Sign up to request clarification or add additional context in comments.

1 Comment

Either dynamic_cast or std::dynamic_pointer_cast.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.