0

I am looking for a way to pass an instance of a templated class to another class.

The context: I am working with a library (that I don't want to edit for now) containing a templated class of this style:

//my templated class
template<int a, int b> //non-type
class Tclass{
   ...
}

I would like to use this class in combination with another library I am currently writing myself. My library contains a class that requires an instance of the templated class. It looks somewhat like this:

//another class that I want to receive the instance of Tclass
class A{
  private:
     Tclass _in; //I want this private variable to hold the passed instance 
  public:
    void A(const Tclass<> & in); //constructor that receives the instance of Tclass
}

//The constructor code:
void A(const Tclass<> & in){
        _in = in; //assign the passed instance to a hidden variable
    }

My main file then would look somewhat like this:

//my main file
int main() {
   Tclass<1,5> tclass; //initialize the templated class with two integers
   A a(tclass); //pass the Tclass instance to another class
   return 0;
}

I saw that on the forum there are a few hints how to do so for a templated class where the variable type is templated, but in my case I have an integer that is templated.

Thank you in advance for your ideas and help.

  • 1
    It's not possible without making either `A` a template as well, or with a redesign. – Some programmer dude Feb 11 '18 at 20:20
  • That would be ok for me, I however did not figure what to template. Would I add template before class A constructor? – LeSinge Feb 11 '18 at 20:22
  • `A` must be the template, that takes the same template parameters, whose constructs takes an instance of the library's template, with the same template pararameters. Either that, or use some kind of type erasure. This is too broad to give a specific answer. Only you know the requirements of your class, and be in the position to figure out the correct approach. – Sam Varshavchik Feb 11 '18 at 20:23

0 Answers0