I have the following code:
It compiles without problems in gcc-3.4, gcc-4.3, the Intel compiler, but does not work under MSVC9.
MSVC reports "using undefined type c_traits<C> when compiling a member function of a class template void foo<C>::go(void) with C = short.
That the compiler is trying to set an unused member function of an unused class, because this class is simply not used at all.
I can get around the problem by specializing in whole foo classes, rather than specializing in its member function. But the fact that specialization of the whole class is a little problematic for me for various reasons.
The big question is: what is right?
- Is my code wrong and the gcc and intel compiler just ignores the problem because they don't install foo completely, or
- The code is correct, and is it an MSVC9 (VC 2008) error that tries to install unused member functions?
The code:
class base_foo { public: virtual void go() {}; virtual ~base_foo() {} }; template<typename C> struct c_traits; template<> struct c_traits<int> { typedef unsigned int_type; }; template<typename C> class foo : public base_foo { public: static base_foo *create() { return new foo<C>(); } virtual void go() { typedef typename c_traits<C>::int_type int_type; int_type i; i=1; } }; template<> base_foo *foo<short>::create() { return new base_foo(); } int main() { base_foo *a; a=foo<short>::create(); delete a; a=foo<int>::create(); delete a; }
c ++ visual-c ++ specialization
Artyom
source share