I have reproduced with a simple code below something I am missing.
Class B owns class A as member and uses same typenames .
I want to avoid replicating these templates so that in the main() I can call something like B b(a, 3.0) which would reuse template from A. Is that possible ?
#include <iostream>
#include <vector>
template<int N, typename T=double>
struct A
{
A(T val) : vecA(N, val) {}
void print() { for (auto i : vecA) std::cout << i << ";"; }
std::vector<T> vecA;
};
template<int N, typename T>
struct B
{
B(const A<N,T> & in, T scal) : a(in), scalB(scal) {}
void print() { a.print(); std::cout << " | " << scalB << std::endl; }
A<N,T> a;
T scalB;
};
int main()
{
A<5,float> a(2.0);
B<5,float> b(a, 3.0); // This is redundancy. Can I do something like B b(a,3.0) ?
b.print();
}
Template parameters can be deduced from the types of a function template's arguments, but not of a class template's constructor arguments. So you could do what the standard library does (for example with make_pair
) and write a function to deduce them for you:
template <int N, typename T>
B<N,T> make_b(const A<N,T> & in, T scal) {
return B<N,T>(in, scal);
}
auto b = make_b(a, 3.0f);
Note that this requires a float argument 3.0f
, to match the type of a
. You could parametrise that type separately, if you want to allow conversions on the second argument:
template <int N, typename T, typename T2>
B<N,T> make_b(const A<N,T> & in, T2 scal) {
return B<N,T>(in, scal);
}
auto b = make_b(a, 3.0);
No, since you create object of type B<int, T>
.
With C++11 you can use auto
and write simple creation function
template<int N, typename T>
B<N, T> create(const A<N, T>& in, T scal)
{
return B<N, T>(in, scal);
}
// in main
auto b = create(a, 3.0f);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With