我有以下代码:

template<class T1, class T2>
class Pair
{
private:
    T1 first;
    T2 second;

public:
    void SetFirst(T1 first)
    {
        this.first = first;
    }
    void SetSecond(T2 second)
    {
        this.second = second;
    }
    T1 GetFirst()
    {
        return first;
    }
    T2 GetSecond()
    {
        return second;
    }
};


我如何实现两个单独的方法SetValue()GetValue(),而不是我拥有的四个方法,它们根据参数决定应使用哪种泛型类型?例如,我在想GetValue()方法可以采用1或2的int参数,并根据数字,返回T1T2类型的变量。但是我事先不知道返回类型,所以有解决办法吗?

最佳答案

不知道您想要什么,而不是确切地了解您的要求,但是...

我建议使用定义如下的包装器基类

template <typename T>
class wrap
 {
   private:
      T  elem;

   public:
      void set (T const & t)
       { elem = t; }

      T get () const
       { return elem; }
 };


现在您的班级可以定义为

template <typename T1, typename T2>
struct Pair : wrap<T1>, wrap<T2>
 {
   template <typename T>
   void set (T const & t)
    { wrap<T>::set(t); }

   template <typename T>
   T get () const
    { return wrap<T>::get(); }
 };


或者,如果您可以使用C ++ 11和可变参数模板,并且定义了类型特征getType以获取列表的第N个类型,

template <std::size_t I, typename, typename ... Ts>
struct getType
 { using type = typename getType<I-1U, Ts...>::type; };

template <typename T, typename ... Ts>
struct getType<0U, T, Ts...>
 { using type = T; };


您可以按以下更灵活的方式定义Pair

template <typename ... Ts>
struct Pair : wrap<Ts>...
 {
   template <typename T>
   void set (T const & t)
    { wrap<T>::set(t); }

   template <std::size_t N, typename T>
   void set (T const & t)
    { wrap<typename getType<N, Ts...>::type>::set(t); }

   template <typename T>
   T get () const
    { return wrap<T>::get(); }

   template <std::size_t N>
   typename getType<N, Ts...>::type get ()
    { return wrap<typename getType<N, Ts...>::type>::get(); }
 };


现在set()的参数可以选择正确的基类和正确的基元素

   Pair<int, long>  p;

   p.set(0);  // set the int elem
   p.set(1L); // set the long elem


否则,通过索引,您可以编写

   p.set<0U>(3); // set the 1st (int) elem
   p.set<1U>(4); // set the 2nd (long) elem


不幸的是,get()没有收到参数,因此必须明确类型(通过类型或通过索引)

   p.get<int>();  // get the int elem value
   p.get<long>(); // get the long elem value

   p.get<0U>(); // get the 1st (int) elem value
   p.get<1U>(); // get the 2nd (long) elem value


显然,当T1等于T2时,这不起作用

以下是(C ++ 11)完整的工作示例

#include <iostream>

template <std::size_t I, typename, typename ... Ts>
struct getType
 { using type = typename getType<I-1U, Ts...>::type; };

template <typename T, typename ... Ts>
struct getType<0U, T, Ts...>
 { using type = T; };

template <typename T>
class wrap
 {
   private:
      T  elem;

   public:
      void set (T const & t)
       { elem = t; }

      T get () const
       { return elem; }
 };

template <typename ... Ts>
struct Pair : wrap<Ts>...
 {
   template <typename T>
   void set (T const & t)
    { wrap<T>::set(t); }

   template <std::size_t N, typename T>
   void set (T const & t)
    { wrap<typename getType<N, Ts...>::type>::set(t); }

   template <typename T>
   T get () const
    { return wrap<T>::get(); }

   template <std::size_t N>
   typename getType<N, Ts...>::type get ()
    { return wrap<typename getType<N, Ts...>::type>::get(); }
 };

int main()
 {
   //Pair<int, int>  p;  compilation error
   Pair<int, long, long long>  p;

   p.set(0);
   p.set(1L);
   p.set(2LL);

   std::cout << p.get<int>() << std::endl;       // print 0
   std::cout << p.get<long>() << std::endl;      // print 1
   std::cout << p.get<long long>() << std::endl; // print 2

   p.set<0U>(3);
   p.set<1U>(4);
   p.set<2U>(5);

   std::cout << p.get<0U>() << std::endl; // print 3
   std::cout << p.get<1U>() << std::endl; // print 4
   std::cout << p.get<2U>() << std::endl; // print 5
 }

09-30 20:36
查看更多