I have a C++ templated class
// Definition
template <typename T>
class MyCLass {
public:
typedef typename T::S MyS; // <-- This is a dependent type from the template one
MyS operator()(const MyS& x);
};
// Implementation
template <typename T>
MyCLass<T>::MyS MyClass<T>::operator()(const MyClass<T>::MyS& x) {...}
What I want is that overloaded operator operator()
behaves differently when MyS
is double
.
I thought about specialization, but how to do in this case considering that the specialization should act on a type-dependent type? Thankyou
You could forward the work to some private overloaded function:
template <typename T>
class MyCLass {
public:
typedef typename T::S MyS;
MyS operator()(const MyS& x) { return operator_impl(x); }
private:
template<typename U>
U operator_impl(const U& x);
double operator_impl(double x);
};
You can solve this by introducing an extra default parameter:
template <typename T, typename Usual = typename T::S>
class MyClass { ... };
Then you can specialize using a double
:
template <typename T>
class MyClass<T, double> { ... }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.