C++核心准则T.61:不要过度参数化成员(SCARY)

时间:2022-07-25
本文章向大家介绍C++核心准则T.61:不要过度参数化成员(SCARY),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

T.61: Do not over-parameterize members (SCARY)

T.61:不要过度参数化成员(SCARY)

Reason(原因)

A member that does not depend on a template parameter cannot be used except for a specific template argument. This limits use and typically increases code size.

不依赖于模板参数的成员无法使用,特定的模板参数除外。这会限制使用并通常会增加代码大小。

Example, bad(反面示例)

template<typename T, typename A = std::allocator{}>
    // requires Regular<T> && Allocator<A>
class List {
public:
    struct Link {   // does not depend on A
        T elem;
        T* pre;
        T* suc;
    };

    using iterator = Link*;

    iterator first() const { return head; }

    // ...
private:
    Link* head;
};

List<int> lst1;
List<int, My_allocator> lst2;

This looks innocent enough, but now Link formally depends on the allocator (even though it doesn't use the allocator). This forces redundant instantiations that can be surprisingly costly in some real-world scenarios. Typically, the solution is to make what would have been a nested class non-local, with its own minimal set of template parameters.

代码看起来足够正确,但是现在Link形式上依赖于分配器(即使它没有使用分配器)。这会引发多余的例示,而这种例示在某些现实流程中可能会带来令人惊讶的高成本。通常的解决方案是让本来的嵌套类别弄成非局部的,同时它的成员只拥有最少的模板参数。

template<typename T>
struct Link {
    T elem;
    T* pre;
    T* suc;
};

template<typename T, typename A = std::allocator{}>
    // requires Regular<T> && Allocator<A>
class List2 {
public:
    using iterator = Link<T>*;

    iterator first() const { return head; }

    // ...
private:
    Link* head;
};

List<int> lst1;
List<int, My_allocator> lst2;

Some people found the idea that the Link no longer was hidden inside the list scary, so we named the technique SCARY. From that academic paper: "The acronym SCARY describes assignments and initializations that are Seemingly erroneous (appearing Constrained by conflicting generic parameters), but Actually work with the Right implementation (unconstrained bY the conflict due to minimized dependencies)."

有些人会发现Link不再被list隐藏,因此我们称这种技术为SCARY。根据大学论文:“SCARY这个缩写描述了一些看起来错误(看起来被冲突的参数约束),但实际上可以和正确的实现一起工作(由于最小化的依赖关系而不会被冲突所限制)的赋值和初始化。”

论文链接:

http://www.open-std.org/jtc1/sc22/WG21/docs/papers/2009/n2911.pdf

Enforcement(实施建议)

  • Flag member types that do not depend on every template argument
  • 标记不依赖于任何模板参数的成员
  • Flag member functions that do not depend on every template argument
  • 标记不依赖于任何模板参数的成员的成员函数。