简体   繁体   English

在派生类中使用基类的模板参数

[英]Using template argument of base class in derived class

Consider the following situation in C++: 考虑C ++中的以下情况:

template<int n>
class Base { ... };

class Derived3 : public Base<3> {
  // a complicated body, making use of n=3
};

class Derived7 : public Base<7> {
  // a completely different body, making use of n=7
};

Inside of the Derived3 member functions, I would like to explicitly use n=3 , and inside Derived7 , n=7 , without hardcoding the numbers, ie, still referring to something like a template argument n . Derived3成员函数内部,我想显式地使用n=3 ,并且在Derived7n=7 ,没有对数字进行硬编码,即仍然指代类似于模板参数n东西。 The following options come to my mind: 我想到了以下选项:

  1. Also templating the derived classes on n , and then using typedef . 还要在n上模板派生类,然后使用typedef This way, the derived classes know n : 这样,派生类知道n

     template<int n> class DerivedTemplate3 : public Base<n> { ... }; typedef DerivedTemplate3<3> Derived3; template<int n> class DerivedTemplate7 : public Base<n> { ... }; typedef DerivedTemplate7<7> Derived7; 

    The problem with this is that DerivedTemplateX makes sense for nothing but n=X , so this feels like abusing the template paradigm. 这个问题是DerivedTemplateXn=X有意义,所以这就像滥用模板范式一样。

  2. Using a static const member to store n in Base , and referring to that in the derived classes: 使用静态const成员将n存储在Base ,并引用派生类中的n:

     template<int n> class Base { protected: static const int nn = n; ... }; class Derived3 : public Base<3> { // refer to nn=3 }; class Derived7 : public Base<7> { // refer to nn=7 }; 

    The problem here is that I seemingly can't use the same identifier ( nn vs. n ). 这里的问题是我似乎不能使用相同的标识符( nnn )。 Also, I'm not sure whether this will allow me to use nn as a template argument for members of the derived classes. 另外,我不确定这是否允许我使用nn作为派生类成员的模板参数。

So: how can this be implemented in a non-redundant, efficient way? 那么:如何以非冗余,有效的方式实施? Maybe using some kind of static const int as a member somewhere? 也许在某处使用某种static const int作为成员?

The standard practice is to use an uppercase letter for the template parameter, then a static const value in lowercase: 标准做法是对模板参数使用大写字母,然后使用小写的静态const值:

template<int N>
class Base {
protected:
  static const int n = N;
  ...
};

Then you use the lowercase static const value n everywhere - don't use N anywhere else. 然后在任何地方使用小写的静态const值n - 不要在其他地方使用N

Also, I'm not sure whether this will allow me to use nn as a template argument for members of the derived classes. 另外,我不确定这是否允许我使用nn作为派生类成员的模板参数。

It is a constant expression and so it can be used as a template argument. 它是一个常量表达式,因此可以用作模板参数。

Does this work for you? 这对你有用吗?

template<int n>
class Base {
protected:
    static const int MyN = n;
};

class Derived3 : public Base<3> {
    void f()
    {
        std::cout << MyN;
    }
};

class Derived7 : public Base<7> {
    void f()
    {
        std::cout << MyN;
    }
};

int main()
{

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM