C ++模板类方法不使用模板参数 [英] C++ Template class methods that do not use template parameter

查看:170
本文介绍了C ++模板类方法不使用模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法M个一类,只有一个方法执行只读一个std ::阵列上操作。的M-1的方法,其余未使用的std ::阵列。低于code。假设我不能打破这种类。

我的担忧是:


  1. 这code是难看一点,因为如上所述,只有1方法使用的参数N

  2. 如果我每个实例化不同氮的酒吧100次,那么不说的膨胀code?就像我说的,只有1方法使用参数N,但我的理解是,我会得到99 *(M-1)有效的相同方法的额外拷贝。

话虽这么说,它是标准使用矢量或C数组来避免模板?我会牺牲任何潜在的性能?

  // bar.h
模板< INT N'GT;
一流的酒吧
{
  上市:
    酒吧(常量的std ::阵列<富,N>&安培; ARR);
    无效method_one();
    无效method_two();
    无效method_three();
    ...
  私人的:
    常量的std ::阵列<富,N> arr_;
};模板< INT N'GT;酒吧和LT; N> ::酒吧(常量的std ::阵列<富,N>&安培; ARR):
  arr_(ARR)
  {}模板< INT N'GT;作废酒吧和LT; N> :: method_one()
{
   // ....
}模板< INT N'GT;作废酒吧和LT; N> :: method_two()
{
   // ....
}//等等


解决方案

首先,你的编译器可能(也可能被诱导)倍,即使他们有不同的签名相同的功能。结果
这是否是合法的(以及如何迫使它),在这里看到:

执行不同的功能有不同的地址?结果
允许网站两个相同的函数定义在同一地址的实现,还是不?

接下来,如果您的模板的成员是独立的模板参数,可以考虑将它们转移到一个基类:

 类Bar_base {
    //此处将一切不依赖于模板参数。
    //可以做多次,如果有用的话
}
模板< INT N'GT; Bar类:公共Bar_base {
    //一切都依赖于`N`,也许有些使用-声明
    //正确超载
}

标准库中的所有的实现做了广泛的,如果你看看< iostream的方式> ,它甚至编纂标准

I have a class with M number of methods, and only one method performs read-only operations on a std::array. The rest of the M-1 methods do not use the std::array. Code below. Assume I can't break up this class.

My concerns are:

  1. This code is little ugly because, as mentioned, only 1 method uses the parameter N.
  2. If I instantiate Bar 100 times each with different N's, then doesn't that bloat the code? Like I said, only 1 method uses the parameter N, yet my understanding is that I'll get 99*(M-1) extra copies of effectively the same method.

That being said, is it standard to use a vector or a C-array instead to avoid template? Will I be sacrificing any potential performance?

//bar.h
template<int N>
class Bar
{
  public:
    Bar(const std::array<Foo, N>& arr);
    void method_one();
    void method_two();
    void method_three();
    ...
  private:
    const std::array<Foo, N> arr_;
};

template<int N> Bar<N>::Bar(const std::array<Foo, N>& arr) :
  arr_(arr)
  {}

template<int N> void Bar<N>::method_one()
{
   //....
}

template<int N> void Bar<N>::method_two()
{
   //....
}

//etc

解决方案

First, your compiler might (or might be induced to) fold identical functions even if they have different signatures.
Whether that is legal (and how to force it), see here:

Do distinct functions have distinct addresses?
Is an implementation allowed to site two identical function definitions at the same address, or not?

Next, if members of your template are independent of template-arguments, consider moving them to a base-class:

class Bar_base {
    // Move everything here not dependent on template-arguments.
    // Can be done multiple times if useful
}
template<int N> class Bar : public Bar_base {
    // Everything dependent on `N`, and maybe some using-declarations
    //  for proper overloading
}

All implementations of the standard library do it extensively, and if you look at <iostream>, it's even codified in the standard.

这篇关于C ++模板类方法不使用模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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