views:

58

answers:

1

I've been trying to implement a function that needs partial template specializations and fallen back to the static struct technique, and I'm having a number of problems.

            template<typename T> struct PushImpl<const T&> {
                typedef T* result_type;
                typedef const T& argument_type;
                template<int StackSize> static result_type Push(IStack<StackSize>* sptr, argument_type ref) {
                // Code if the template is T&
                }
            };
            template<typename T> struct PushImpl<const T*> {
                typedef T* result_type;
                typedef const T* argument_type;
                template<int StackSize> static result_type Push(IStack<StackSize>* sptr, argument_type ptr) {
                    return PushImpl<const T&>::Push(sptr, *ptr);
                }
            };
            template<typename T> struct PushImpl {
                typedef T* result_type;
                typedef const T& argument_type;
                template<int StackSize> static result_type Push(IStack<StackSize>* sptr, argument_type ref) {
                // Code if the template is neither T* nor T&
                }
            };

            template<typename T> typename PushImpl<T>::result_type Push(typename PushImpl<T>::argument_type ref) {
                return PushImpl<T>::Push(this, ref);
            }

First: The struct is nested inside another class (the one that offers Push as a member func), but it can't access the template parameter (StackSize), even though my other nested classes all could. I've worked around it, but it would be cleaner if they could just access StackSize like a normal class.

Second: The compiler complains that it doesn't use or can't deduce T. Really?

Thirdly: The compiler complains that it can't specialize a template in the current scope (class scope).

I can't see what the problem is. Have I accidentally invoked some bad syntax?

+4  A: 

The general case must appear before the specializations, otherwise the specializations have nothing to specialize.

Cogwheel - Matthew Orlando
Uck, I hate the standard.
DeadMG