类模板中的静态方法模板的专门化 [英] specialization of static method template in class template

查看:176
本文介绍了类模板中的静态方法模板的专门化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使此代码有效?

  template< typename T& class test {
public:
template< typename T2> static void f(){
cout<<generic<< endl;
}
模板<> static void f< void>(){//在具有参数T的通用类模板内的模板T2的静态方法的专门化。
}
};

如果不是,是因为它被视为一个函数的局部模板特殊化?

解决方案

正如其他人在评论和链接中指出的那样,当前的C ++模板语法没有简单的方法来支持这种用法。但是,如果你真的想这样做,你不介意一些复杂的读。



你必须处理的两个主要问题是:


  1. 功能模板不支持部分专业化。

  2. 部分专业化在您将其定义为


  3. 要绕过他们,接近你想要的东西,你可以试试。 / p>


    • 将模板定义移到类外。

    • 将这些模板定义为类函数






     模板<类型名T> 
    class test
    {
    public:
    template< typename T2>
    static void f();
    };

    template< typename T1,typename T2>
    struct f
    {
    void operator()(void){cout< generic< endl }
    };

    template< typename T1>
    struct f< T1,void>
    {
    void operator()(void){cout< void< endl }
    };

    template< typename T>
    template< typename T2>
    void test< T> :: f()
    {
    :: f< T,T2>()
    }

    int main()
    {
    test< int> :: f< int> // prints'generic'
    test< int> :: f< void>(); // prints'void'
    }

    这是一个很多额外的代码像这样,但我想如果你想做到这一点够糟糕,这是一种可能性。


    is there a way to make this code work?

    template<typename T> class test{
    public:
        template<typename T2> static void f(){
            cout<<"generic"<<endl;
        }
        template<> static void f<void>(){    //specialization of static method for template T2, within generic class template with argument T
            cout<<"void"<<endl;
        }
    };
    

    if not, is it because it counts as a partial template specialization of a function?

    解决方案

    As others have pointed out in the comments and link, the current C++ template syntax provides no easy way to support this usage. However, if you really want to do this and you don't mind introducing some complexity read on.

    The two main problems you have to deal with are:

    1. function templates don't support partial specialization.
    2. partial specialization doesn't work when you define it as part of a class scope.

    To get around them and get close to what you're looking for here's what you can try.

    • move the template definition outside of the class.
    • define those templates as class functors instead to permit partial spec'ing.

    template<typename T>
    class test
    {
    public:
      template <typename T2>
      static void f();
    };
    
    template <typename T1, typename T2>
    struct f
    {
        void operator ()(void) { cout << "generic" << endl; }
    };
    
    template <typename T1>
    struct f<T1, void>
    {
        void operator ()(void) { cout << "void" << endl; }
    };
    
    template <typename T>
      template <typename T2>
      void test<T>::f()
      {
        ::f<T, T2>()();
      }
    
    int main()
    {
      test<int>::f<int>();      // prints 'generic'
      test<int>::f<void>();     // prints 'void'
    }
    

    That's an awful lot of extra code introduced for something like this but I suppose if you want to do this bad enough it's a possibility.

    这篇关于类模板中的静态方法模板的专门化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆