类模板部分特化问题

时间:2010-06-17 21:48:18

标签: c++ templates specialization partial-specialization

我一直在尝试实现一个需要部分模板特化并回归静态结构技术的函数,而且我遇到了很多问题。

            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);
            }

首先:struct嵌套在另一个类(提供Push作为成员func的那个)中,但它无法访问模板参数(StackSize),即使我的其他嵌套类都可以。我已经解决了这个问题,但如果他们能够像普通类一样访问StackSize,它会更清晰。

第二:编译器抱怨它不使用或不能推断T.真的吗?

第三:编译器抱怨它无法在当前范围(类范围)中专门化模板。

我看不出问题所在。我是否意外地调用了一些错误的语法?

1 个答案:

答案 0 :(得分:4)

一般情况必须出现在专业化之前,否则专业化没有任何专业化。