Is there a way to define a template member in a non-template class?
How about (untested):
class Function_A
{
public:
template <typename T> void Evaluate(T parameter)
{
T value = std::get<Compute<T>>(computers).Function_B(parameter);
return T(SomeParameter) * value;
}
private:
double SomeParameter;
std::tuple<Compute<type_1>, Compute<type_2>> computers;
};
Note: std::pair
would work exactly the same as std::tuple
here, if you fancy the first/second semantics it adds.
Additionally, note that T(SomeParameter)
is a C-style cast, which could be problematic if T
is not a class type. Consider T{}
or static_cast<T>()
.
One thing you can do is make C
static
. If you have
template <typename T> void Evaluate(T parameter)
{
static Compute<T> C; // only do this once per T now
T value = C.Function_B(parameter);
return T(SomeParameter)*value;
}
then when you call Evaluate
with type_1
you'll have one version of the function that has C<type_1>
in it that will only be constructed the first time the function is called and the same thing happens for type_2
.