Specialized Template Template

My brain melted over the course of several weeks of 14-hour days.

I have a template class, and I'm trying to write a template conversion constructor for this class and specialize this constructor. The compiler (MSVC9) is completely dissatisfied with me. This is a minimal example of the actual code I'm trying to write. A compiler error is built into the code.

Help me not to melt my brain. What syntax do I need to do what I'm trying to do? NOTE. In my real code, I have to define a conversion constructor outside the declaration, so this is not an option for me.

#include <string> #include <sstream> using namespace std; template<typename A> class Gizmo { public: Gizmo() : a_() {}; Gizmo(const A& a) : a_(a) {}; template<typename Conv> Gizmo(const Conv& conv) : a_(static_cast<A>(conv)) {}; private: A a_; }; // // ERROR HERE: // " error C2039: 'Gizmo<B>' : is not a member of 'Gizmo<A>'" // template<> template<typename B> Gizmo<string>::Gizmo<typename B>(const B& b) { stringstream ss; ss << b; ss >> a_; } int main() { Gizmo<int> a_int; Gizmo<int> a_int2(123); Gizmo<string> a_f(546.0f); return 0; } 
+7
c ++ templates template-specialization
source share
1 answer
 template<> template<typename B> Gizmo<string>::Gizmo(const B& b) 

Also note that the typename keyword from const typename B& must be removed.

+7
source share

All Articles