0

is it possible to cast a variable to a templated subclass without knowing the template type? This is so that I can access a template property within the subclass. Here is some code and comments to help explain.

class base {...};
template <class T>
class sub_class : public base{
public:
    T* /*or shared_ptr*/ value = nullptr;
};

int main() {
    base* b = new sub_class<int>();
    //I know b is a sub_class<int>,
    //but is there any way I can cast it as a sub_class
    //without knowing the that it took in an int
    auto* s = (sub_class*)b;//This doesn't work but is
    //there anything that does something similar to what
    //I'm intending. Also, feel free to correct this 
    //line, I don't know much about using auto.
}

Any help is appreciated, including whether or not it's possible, any alternatives to what I want to achieve, etc. Thanks!

Apoqlite
  • 239
  • 2
  • 21
  • Your question is somewhat unclear. "sub_class" is not a type or a class that can be converted to. It's a template. A template is not a class or an object with a type. You need to explain in more detail exactly what you're trying to accomplish. – Sam Varshavchik Jun 06 '20 at 21:58
  • The type of `auto` is deduced at compile time, so no. Instead consider using polymorphism. Perhaps if you elaborated a bit more on what you're trying to do. – cdhowie Jun 06 '20 at 21:59
  • No, there's no way to know, from `base*` pointer alone, what actual object it points to. If `base` has virtual methods, you could test various possibilities with `dynamic_cast`, but you'd need to know them in advance. The fact that the derived classes are named `sub_class` and `sub_class` rather than `Derived1` and `Derived2` is irrelevant; templates don't change anything here. – Igor Tandetnik Jun 06 '20 at 22:00
  • @cdhowie I wanted to do this because in order to store templated classes in a vector I need to create a class, from which the templated class is derived from. So I keep on having to face the issue of casting the variable, and then finally I had to implement a method where I don't know the specifications of the template. Would you suggest any possible alternatives for doing this? – Apoqlite Jun 06 '20 at 22:10
  • Imagine for a moment that the code you show works somehow. What would you do with `s` next? How do you plan to use it? – Igor Tandetnik Jun 06 '20 at 22:12
  • @IgorTandetnik I would access a template (same template as the 'cast') pointer specific to the templated class and check if it is nullptr. So yeah, it's sort of a small accessing task, but that value is only in the subclass. – Apoqlite Jun 06 '20 at 22:18
  • What is "template pointer specific to the templated class"? I'm not sure I understand. Could you update your code with an example of what's inside `sub_class`, and how you are going to use `s`? – Igor Tandetnik Jun 06 '20 at 22:21
  • @IgorTandetnik added the variable to the subclass. This is what I wanted to access, but I can't unless I cast the variable it. So if `s` was possible, then I would want to see `if (s->value != nullptr) {...}` – Apoqlite Jun 06 '20 at 22:24
  • Add a `virtual bool isPointerNull() const = 0;` to `base`. In `sub_class`, write `bool isPointerNull() const override { return !value; }`. Now you can check whether `value` is null with just `base*` pointer in hand. – Igor Tandetnik Jun 06 '20 at 22:27
  • @IgorTandetnik Perfect! Thank you so much, IDK why I couldn't think of this! – Apoqlite Jun 06 '20 at 22:29
  • That is called type erasure and is described here: [What is type erasure in C++?](https://stackoverflow.com/questions/34815513/what-is-type-erasure-in-c). It's a very common technique; e.g. it's how `std::function` is usually implemented. – HTNW Jun 06 '20 at 22:30

0 Answers0