|
Prev: check file opened or not?
Next: Init COM interfaces
From: Jack Hanebach on 2 Apr 2008 13:16 Given: template<typename T1, typename T2> struct A { void foo() {} }; we can specialize the A::foo() as follow: template<> void A<double, int>::foo() { } or we can partially specialize A template<typename T> struct A<T, double> { void foo() {} }; Yet, according to VC8 and comeau, we cannot partially specialize A::foo() like that: template<typename T> void A<T, double>::foo() {} Am I using the incorrect syntax, or is that kind of partial specialization illegal? If so, anybody has any Idea why? // since I need special behavior for only one method, // I don't really want to specialize the whole class // and have to 'copy and paste' the other methods...
From: Ben Voigt [C++ MVP] on 2 Apr 2008 13:45 Jack Hanebach wrote: > Given: > > template<typename T1, typename T2> > struct A > { > void foo() {} > }; > > we can specialize the A::foo() as follow: > > template<> > void A<double, int>::foo() > { > } > > or we can partially specialize A > > template<typename T> > struct A<T, double> > { > void foo() {} > }; > > Yet, according to VC8 and comeau, we cannot partially > specialize A::foo() like that: > > template<typename T> > void A<T, double>::foo() {} > > Am I using the incorrect syntax, or is that kind of partial > specialization illegal? If so, anybody has any Idea why? > > // since I need special behavior for only one method, > // I don't really want to specialize the whole class > // and have to 'copy and paste' the other methods... Inheritance often helps reuse the same behavior in several partial specializations.
From: Victor Bazarov on 2 Apr 2008 13:40 Jack Hanebach wrote: > [..] > Am I using the incorrect syntax, or is that kind of partial > specialization illegal? If so, anybody has any Idea why? Partial specialisations of functions are prohibited. V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask
From: Jack Hanebach on 2 Apr 2008 13:55 Ben Voigt [C++ MVP] wrote: > Inheritance often helps reuse the same behavior in several partial > specializations. Yes, I know. Actually that's how I solved my problem. By factoring the original design into 'policy' classes. I was just curious what might be the reason for disallowing what is essentially just a shortcut for partial specialization of the class, when we neeed changed behavior in only some methods. --
From: Igor Tandetnik on 2 Apr 2008 22:21
"Jack Hanebach" <firstname(a)lastname.net> wrote in message news:u53ZcVOlIHA.4712(a)TK2MSFTNGP04.phx.gbl > Given: > > template<typename T1, typename T2> > struct A > { > void foo() {} > }; > > > Yet, according to VC8 and comeau, we cannot partially > specialize A::foo() like that: > > template<typename T> > void A<T, double>::foo() {} There ain't no such thing as partial specialization of function templates. You can simulate it by delegating to a class template, which in turn can be partially specialized: template <typename T1, typename T2> struct FooImpl { static void foo(A* pThis) {}; }; template<typename T1, typename T2> struct A { void foo() { FooImpl<T1, T2>::foo(this); } }; // Partial specialization template<typename T> struct FooImpl<T, double> { static void foo(A* pThis) {} }; -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925 |