I have a templated class that needs a specialized constructor when the template parameters is the same type as the class. The code below won't compile.
What's the correct syntax for specifying the use of a particular constructor when the type is of Dual ? In particular, I need to initialize the member 'real' on the initializer list when the template parameter is of type Dual, but not when it isn't (type double for example).
template<class X> class Dual {
public:
X real;
size_t N;
std::vector<X> imag;//don't know N at compile time
Dual(size_t _N);
};
template <class X>
inline Dual<X>::Dual(size_t _N): N(_N), imag(N, 0.0) {}
template <class X>
inline Dual<Dual<X> >::Dual(size_t _N): real(_N), N(_N), imag(_N, 0.0) {}
//syntax error:
//error: cpptest.cpp:20:24: error: C++ requires a type specifier for all declarations
//inline Dual<Dual<X> >::Dual(size_t _N): real(_N), N(_N), imag(_N, 0.0) {}
//~~~~~~
int main(){
Dual <double> a(5);
Dual< Dual < double>> b(5);
}