前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则T.42:使用模板别名简化记法并隐藏实现细节

C++核心准则T.42:使用模板别名简化记法并隐藏实现细节

作者头像
面向对象思考
发布2020-09-10 10:41:51
4840
发布2020-09-10 10:41:51
举报

君子兰

T.42: Use template aliases to simplify notation and hide implementation details

T.42:使用模板别名简化记法并隐藏实现细节

Reason(原因)

Improved readability. Implementation hiding. Note that template aliases replace many uses of traits to compute a type. They can also be used to wrap a trait.

提高可读性。隐藏实现。注意模板别名代替很多用于计算类型特征的使用。它们可以用于封装特征。

Example(示例)

template<typename T, size_t N>
class Matrix {
    // ...
    using Iterator = typename std::vector<T>::iterator;
    // ...
};

This saves the user of Matrix from having to know that its elements are stored in a vector and also saves the user from repeatedly typing typename std::vector<T>::.

Matrix的用户不必知道它的元素存存储于于vector,也让用户不必重复输入类型名std::vecttor<T>::。

Example(示例)

template<typename T>
void user(T& c)
{
    // ...
    typename container_traits<T>::value_type x; // bad, verbose
    // ...
}

template<typename T>
using Value_type = typename container_traits<T>::value_type;

This saves the user of Value_type from having to know the technique used to implement value_types.

Value_type的用户不必了解实现value_type的技术。

template<typename T>
void user2(T& c)
{
    // ...
    Value_type<T> x;
    // ...
}
Note(注意)

A simple, common use could be expressed: "Wrap traits!"

简单,普通的用法可以被解释为“封装特征”。

Enforcement(实施建议)

  • Flag use of typename as a disambiguator outside using declarations.
  • 标记在using外面将typename作为消歧器使用的情况。
  • ???

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#t42-use-template-aliases-to-simplify-notation-and-hide-implementation-details

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-09-05,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 君子兰
  • T.42: Use template aliases to simplify notation and hide implementation details
  • Reason(原因)
    • Note(注意)
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档