Pup*_*ppy 1 c++ templates partial-specialization 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);
}
Run Code Online (Sandbox Code Playgroud)
第一:该结构嵌套在另一个类(提供 Push 作为成员 func 的类)中,但它无法访问模板参数 (StackSize),即使我的其他嵌套类都可以。我已经解决了这个问题,但是如果他们可以像普通类一样访问 StackSize 会更干净。
第二:编译器抱怨它不使用或不能推导出 T。真的吗?
第三:编译器抱怨它不能在当前范围(类范围)中专门化模板。
我看不出是什么问题。我是不是不小心调用了一些不好的语法?