类模板部分特化的问题

发布于 2024-09-05 18:43:10 字数 1646 浏览 4 评论 0原文

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

            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。真的吗?

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木落 2024-09-12 18:43:10

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文