Is it possible to have a templated class and also templating the constructor with some other type?
something like this:
template<typename T1>
class Foo{
template<typename T2>
Foo(T1 aBar, T2 dummyArgument){
bar = aBar;
bytesOfT2 = sizeof(T2);
};
int bytesOfT2;
T1 bar;
};
is this possible? and if so, how would I call such a constructor? Do I need to consider something in regards of header and cpp files?
thanks!
//edit: my particular example is actually even a little bit more complicated. i have
template <typename U1, U2>
class Foo{
U1 var1;
U2 var2;
};
template <typename T1>
class Bar{
template<typename T2, typename T3>
Bar(Foo<T2,T3> aFoo, T1 aVal){
val=aVal;
bytesOfT2=sizeof(T2);
bytesOfT3=sizeOf(T3);
};
int bytesOfT2;
int bytesOfT3;
T1 val;
};
does it mean i can here call the constructor just with any variable of type Foo and it should automatically select the proper Constructor acording to the particular version of Foo (for example if the variable i pass is of type Foo should it automatically set T2 to bool and T3 to float)?
Yes, a class template can have a constructor template. You call it as you would call any other constructor:
This invokes the constructor template with
T1 = int(becauseT1is a class template parameter and the class template argument isint) andT2 = double(becauseT2is a function template argument and is deduced from the argument0.0).All of the template arguments have to be able to be deduced from the function arguments, otherwise the constructor template cannot be called. There is no way to explicitly specify the template arguments for a constructor template.