C++核心准则T.62:将非依赖类模板成员放入非模板基类中

时间:2022-07-25
本文章向大家介绍C++核心准则T.62:将非依赖类模板成员放入非模板基类中,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

T.62: Place non-dependent class template members in a non-templated base class

T.62:将非依赖类模板成员放入非模板基类中

Reason(原因)

Allow the base class members to be used without specifying template arguments and without template instantiation.

允许在不定义模板参数和不例示模板的情况下使用基类成员。

Example(示例)

template<typename T>
class Foo {
public:
    enum { v1, v2 };
    // ...
};
struct Foo_base {
    enum { v1, v2 };
    // ...
};

template<typename T>
class Foo : public Foo_base {
public:
    // ...
};
Note(注意)

A more general version of this rule would be "If a class template member depends on only N template parameters out of M, place it in a base class with only N parameters." For N == 1, we have a choice of a base class of a class in the surrounding scope as in T.61.

What about constants? class statics?

本规则的更普遍版是:如果模板类成员只依赖于M以外的N个模板参数,将其放入只包含N个参数的基类中。对于N==1的情况,我们可以选择外围作用域的某个类的基类,就像T.61那样。

常量该如何处理?静态成员呢?

Enforcement(实施建议)

  • Flag
  • 标记