0

Is it possible to add a template class inside std::array without specifying the typename? I mean.

template<typename T>
class MyClass
{
    ...
}

std::array<MyClass *> arr;

The reason is that I have a kind of storage that accepts all classes that derives from MyClass but the problem with the template class is that I need to specify the typename, then the class need to be like that:

class Storage
{
    ...
private:
    std::array<MyClass<TYPE GOES HERE> *> arr;
}

And I want something more or less like this:

class Storage
{
    ...
private:
    std::array<MyClass *> arr;
}

This way I can add any class that derives from MyClass.

Is there a way for doing that?

18
  • 1
    One option is making Storage a template class as well. Commented Sep 16, 2014 at 21:29
  • 5
    MyClass<int> and MyClass<double> are completely unrelated types. If you want an array that can store pointers to both, give them a common base class or use type erasure. Commented Sep 16, 2014 at 21:31
  • 6
    There are no "template classes" in C++. There are only class templates - templates which can be used to produce classes. Templates (class templates) are not classes themselves ("You can't eat the cookie cutter" - STL) Commented Sep 16, 2014 at 21:38
  • 2
    If your code allows it you can create non template MyBaseClass and derive MyClass from it so that Storage will hold std::array<MyBaseClass *> arr;. If you need to call T-specific functions of MyClass from Storage - then you can't do this, and basically can't make Storage class non-T-specific. Commented Sep 16, 2014 at 21:38
  • 2
    " I'm just wondering if there's a way for doing that." Deriving from a common base IS. THE. WAY. Of doing "that". Commented Sep 16, 2014 at 21:43

2 Answers 2

3

One option is to create a base class from which MyClass derives and let the array store pointers to the base class.

struct MyBase
{
   virtual ~Base() {}
};

template<typename T>
class MyClass : public MyBase
{
    ...
}

std::array<MyBase*> arr;
Sign up to request clarification or add additional context in comments.

1 Comment

Despite others members helped me and some of them (such as the answer below), mentioned that technique, and yes, it worked and seems to be the best. The only thing is that to use methods of classes that derives from MyClass is that I need to declare on MyBase before. Polymorphism.
2

The reason is that I have a kind of storage that accepts all classes that derives from MyClass

You cannot create a class that derives from MyClass, you can create a class that derives from MyClass<int> for example etc. So solutions that I see are:

  • Make template MyClass derived from non template BaseClass and keep pointer to BaseClass
  • Make MyClass non template and create a template class that derives from it. (this is technically the same as the first)
  • Keep void * and cast it to specific MyClass<> on usage, this is error prone though and not recommended.
  • Use boost::any or boost::variant (using boost::variant would be pretty complicated though

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.