Issue with class template partial specialization

Posted by DeadMG on Stack Overflow See other posts from Stack Overflow or by DeadMG
Published on 2010-06-17T21:48:17Z Indexed on 2010/06/17 21:53 UTC
Read the original article Hit count: 365

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about c++

Related posts about template