gpt4 book ai didi

c++ - 类模板部分特化问题

转载 作者:行者123 更新时间:2023-11-27 23:33:28 27 4
gpt4 key购买 nike

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

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

第一:该结构嵌套在另一个类(提供 Push 作为成员函数的类)中,但它无法访问模板参数 (StackSize),即使我的其他嵌套类都可以。我已经解决了它,但如果他们可以像普通类一样访问 StackSize 会更干净。

其次:编译器​​提示它没有使用或不能推导 T。真的吗?

第三:编译器提示它不能特化当前作用域(类作用域)的模板。

我看不出问题是什么。我是否不小心调用了一些错误的语法?

最佳答案

一般情况必须出现在特化之前,否则特化就没有什么可特化的。

关于c++ - 类模板部分特化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3065903/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com