Skip to main content
GameDev.net gamedev.net
🔒 Locked

Partial Template Specialization WTF

Started by Max_Payne Jun 21, 2006 at 2:35 PM 1 replies 2.1k views
Original Post
Max_Payne
Max_Payne
I am working with VC++ 2003 and I get an error which makes no sense to me, and the MSDN pages are not helping at all. If someone would be kind enough to suggest a fix...


template <size_t A>
void foo()
{
}

template <>
void foo<0>()
{
}

template <size_t A, size_t B>
void bar()
{
}

template <size_t A>
void bar<A, 0>()
{
}


The error is as follows: error C2768: 'bar' : illegal use of explicit template arguments It applies to my attempt at a partial specialization of the second function. While I can specialize foo, which has one template argument, it seems impossible to specialize bar over one of its template arguments.

Looking for a serious game project?
www.xgameproject.com
Fruny
Fruny
You cannot partially specialize a function template.

Workaround:
template<size_t A, size_t B>struct bar_helper{   static void bar();};template<size_t A, size_t B>void bar_helper<A,B>::bar(){}template<size_t A>void bar_helper<A,0>::bar(){}template <size_t A, size_t B>void bar(){   bar_helper<A,B>::bar()}
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Polymorphic OOP
Polymorphic OOP
Quote:
Original post by Fruny
Workaround:

You can't define a member function of a partially specialized type template unless the partial specialization of the type is defined, so for the above code to work, prior to your second bar member function defintion you'd need to do:

template<size_t A>struct bar_helper< A, 0 >{   static void bar();};


Another alternative would be to use SFINAE to avoid requiring a helper struct. If you have boost installed, this can easily be done with enable_if:

#include <boost/utility/enable_if.hpp>template <size_t A, size_t B>typename ::boost::disable_if_c<(B==0)>::type bar(){  // Definition if B != 0}template <size_t A, size_t B>typename ::boost::enable_if_c<(B==0)>::type bar(){  // Definition if B == 0}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.