前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则T.61:不要过度参数化成员(SCARY)

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

作者头像
面向对象思考
发布2020-09-21 14:43:01
2700
发布2020-09-21 14:43:01
举报
文章被收录于专栏:C++核心准则原文翻译

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(反面示例)

代码语言:javascript
复制
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形式上依赖于分配器(即使它没有使用分配器)。这会引发多余的例示,而这种例示在某些现实流程中可能会带来令人惊讶的高成本。通常的解决方案是让本来的嵌套类别弄成非局部的,同时它的成员只拥有最少的模板参数。

代码语言:javascript
复制
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
  • 标记不依赖于任何模板参数的成员的成员函数。
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-09-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档