【问题标题】:C++ factoring tempate methods specialization of a template class, is that possible?模板类的C++工厂模板方法特化,这可能吗?
【发布时间】:2015-02-09 17:10:45
【问题描述】:

我在模板类中有一个模板方法。
我读过之前没有专门化类就无法专门化方法。
但是我想分解其中一些方法,可以吗?

示例:

class One {
public:
  static const int number = 1;
};

class Two {
public:
  static const int number = 2;
};    

template<typename num> class A  {
private:
  num n;

public:
  template<typename type>
  void multiplyBy(); // by 1 if <int> or 1,5 if <float>
}; // A

template<> template<> void A<One>::multiplyBy<int>() {
  std::cout << 1.0*n.number << std::endl;
}

template<> template<> void A<One>::multiplyBy<float>() {
  std::cout << 1.5*n.number << std::endl;
}

template<> template<> void A<Two>::multiplyBy<int>() {
  std::cout << 1.0*n.number << std::endl;
}

template<> template<> void A<Two>::multiplyBy<float>() {
  std::cout << 1.5*n.number << std::endl;
}

int main() {
  A<One> aOne;
  A<Two> aTwo;
  aOne.multiplyBy<int>();   // 1
  aOne.multiplyBy<float>(); // 1.5
  aTwo.multiplyBy<int>();   // 2
  aTwo.multiplyBy<float>(); // 3

  return 0;
}

stackoverflow 相关问题:C++ specialization of template function inside template class
特别是这条评论:C++ specialization of template function inside template class

我是否必须扣除没有办法分解multiplyBy(),一个用于int,另一个用于float? 由于英语不是我的自然语言,也许我错过了一些简单的东西,也许是部分专业化的解决方法。

编辑:将 A::n 设为私有以更好地匹配我的问题。

【问题讨论】:

    标签: c++ templates refactoring template-specialization


    【解决方案1】:

    你可以使用标签调度:

    #include <iostream>
    
    class One {
    public:
      static const int number = 1;
    };
    
    class Two {
    public:
      static const int number = 2;
    };
    
    template<typename num>
    class A  {
        public:
        num n;
    
        private:
        template<typename> struct Tag {};
    
        void multiplyBy(Tag<int>) {
            std::cout << 1.0*n.number << std::endl;
        }
    
        void multiplyBy(Tag<float>) {
            std::cout << 1.5*n.number << std::endl;
        }
    
        public:
        template<typename type>
        void multiplyBy() {
            multiplyBy(Tag<type>());
        }
    };
    
    
    int main() {
      A<One> aOne;
      A<Two> aTwo;
      aOne.multiplyBy<int>();   // 1
      aOne.multiplyBy<float>(); // 1.5
      aTwo.multiplyBy<int>();   // 2
      aTwo.multiplyBy<float>(); // 3
    
      return 0;
    }
    

    【讨论】:

      【解决方案2】:

      但是我想分解其中的一些方法,可以吗?

      你可能知道你不能使用:

      template<> template<> void A<One>::multiplyBy<int>() {
        std::cout << 1.0*n.number << std::endl;
      }
      

      没有专门的A&lt;One&gt;

      您可以按照以下方式做一些事情:

      #include <iostream>
      
      class One {
      public:
        static const int number = 1;
      };
      
      class Two {
      public:
        static const int number = 2;
      };    
      
      template<typename num, typename type = int> struct MultiplyBy {
         static void doit(num n)
         {
            std::cout << 1.0*n.number << std::endl;
         }
      };
      
      template<typename num> struct MultiplyBy<num, float> {
         static void doit(num n)
         {
            std::cout << 1.5*n.number << std::endl;
         }
      };
      
      template<typename num> class A  {
      public:
        num n;
      
        template<typename type>
        void multiplyBy()
        {
           MultiplyBy<num, type>::doit(n);
        }
      };
      
      
      int main() {
        A<One> aOne;
        A<Two> aTwo;
        aOne.multiplyBy<int>();   // 1
        aOne.multiplyBy<float>(); // 1.5
        aTwo.multiplyBy<int>();   // 2
        aTwo.multiplyBy<float>(); // 3
      
        return 0;
      }
      

      【讨论】:

      • 这个答案在我的特殊情况下不起作用。 MultiplyBy() 必须访问 A 类的私人成员。我的问题不够准确。
      • MultiplyBy 不需要访问A 的私有成员。当A::multiplyBy() 调用MultiplyBy::doit() 时,它按值传递其私有成员数据。
      猜你喜欢
      • 2019-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-14
      • 2020-12-24
      • 2010-12-15
      • 2021-10-15
      相关资源
      最近更新 更多