簡體   English   中英

如何部分專門化非模板類的模板成員方法?

[英]How can I partially specialise a templated member method of a non-templated class?

對於模板類的部分專業化,我可以找到很多幫助,但是我想對非模板類的方法進行部分專業化。

class TempMembMeth {
  public:
    template<typename T1, typename T2>
    void templMethod1(int);
};

我的模板化方法實現:

template<typename T1, typename T2>
  void TempMembMeth::templMethod1(int){
  std::cout << "<T1,T2>templMethod1(int)" << '\n';
}

我可以完全指定方法確定:

template<>
inline void TempMembMeth::templMethod1<char,char>(int){
    std::cout << "<char,char>templMethod1(int)" << '\n';
}

但是,我該如何做部分說明?

template<typename T2>
inline void TempMembMeth::templMethod1<char,T2>(int){
    std::cout << "<char,?>templMethod1(int)" << '\n';
}

我得到:'TempMembMeth :: templMethod1':顯式模板參數的非法使用我不能使用部分專用的方法重新定義該類,因為該類未進行模板化。 它也不會讓我“重載”類中的模板化方法-它再次抱怨非法使用顯式模板參數。

那么有什么想法可能嗎? 目前,我已經通過模板化類來解決這個問題,但是我想知道如果沒有模板化類,這是否可能實現。

正如TC所評論的那樣,您不能部分專門化模板功能。

您應該改用重載。 如有必要,可以添加一些帶有Type2Type參數的私有函數。 就像是:

#include <iostream>

struct Test
{
public:

    template<typename T1, typename T2>
    void templMethod1(int i)
    {
        templMethodImpl(i, Type2Type<T1>(), Type2Type<T2>());
    }

private:

    template<typename T>
    struct Type2Type
    {
        typedef T OriginalType;
    };

    template<class T1, class T2>
    void templMethodImpl(int i, Type2Type<T1>, Type2Type<T2>)
    {
        std::cout << "t1 any, t2 any" << std::endl;
    }

    template<class T2>
    void templMethodImpl(int i, Type2Type<char>, Type2Type<T2>)
    { 
        std::cout << "t1 char, t2 any" << std::endl;
    }

    void templMethodImpl(int i, Type2Type<int>, Type2Type<char>)
    {
        std::cout << "t1 int, t2 char" << std::endl;
    }
};

int main()
{
    Test test;

    test.templMethod1<char, char>(5);
    test.templMethod1<int, char>(5);
    test.templMethod1<char, int>(5);
    test.templMethod1<int, int>(5);
}

正如其他人已經指出的那樣,您不能部分專門化功能模板。 但是,您可以使用效果幾乎相同的輔助類。

#include <iostream>

namespace details
{
   // Forward declaration of the helper class.
   template<typename T1, typename T2> struct Helper;
}

class TempMembMeth 
{
  public:

    template<typename T1, typename T2>
    void templMethod1(int arg)
    {
       details::Helper<T1, T2>::doit(this, arg);
    }
};

namespace details
{
   // Implement the helper class and its specializations.
   template<typename T1, typename T2> struct Helper
   {
      static void doit(TempMembMeth* obj, int arg)
      {
         std::cout << "In Helper<T1,T2>::doit()\n";
      }
   };

   template<typename T2> struct Helper<char, T2>
   {
      static void doit(TempMembMeth* obj, int arg)
      {
         std::cout << "In Helper<char,T2>::doit()\n";
      }
   };

   template<> struct Helper<char, char>
   {
      static void doit(TempMembMeth* obj, int arg)
      {
         std::cout << "In Helper<char, char>::doit()\n";
      }
   };
}

int main()
{
   TempMembMeth obj;
   obj.templMethod1<float, int>(10);
   obj.templMethod1<char, double>(20);
   obj.templMethod1<char, char>(30);
   return 0;
}

輸出:

In Helper<T1,T2>::doit()
In Helper<char,T2>::doit()
In Helper<char, char>::doit()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM