T.44: Use function templates to deduce class template argument types (where feasible)

T.44:使用函数模板推断类模板参数类型(如果可能)

Reason(原因)

Writing the template argument types explicitly can be tedious and unnecessarily verbose.

显示输入模板参数类型冗长且无必要。

Example(示例)

tuple t1 = {1, "Hamlet", 3.14};   // explicit typeauto t2 = make_tuple(1, "Ophelia"s, 3.14);         // better; deduced type

Note the use of the s suffix to ensure that the string is a std::string, rather than a C-style string.

注意通过使用s后缀可以保证string是std::string而不是C风格字符串。

Note(注意)

Since you can trivially write a make_T function, so could the compiler. Thus, make_T functions might become redundant in the future.

你可以直接编写make_T函数,编译器也可以。因此make_T函数将来可能会变得多余。

Exception(例外)

Sometimes there isn't a good way of getting the template arguments deduced and sometimes, you want to specify the arguments explicitly:

有时,没有合适的方式实现模板参数推断,也有可能你希望显式定义参数类型。

vector v = { 1, 2, 3, 7.9, 15.99 };list lst;
Note(注意)

Note that C++17 will make this rule redundant by allowing the template arguments to be deduced directly from constructor arguments: Template parameter deduction for constructors (Rev. 3). For example:

注意C++17将会令本规则多余,原因是C++17允许直接通过构造函数参数直接推断模板参数:构造函数的模板参数推断(Rev.3)。例如:

tuple t1 = {1, "Hamlet"s, 3.14}; // deduced: tuple
Enforcement(实施建议)

Flag uses where an explicitly specialized type exactly matches the types of the arguments used.

标记显示定义的类型和实际使用的参数完全匹配的情况。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#t44-use-function-templates-to-deduce-class-template-argument-types-where-feasible

新书介绍

《实战Python设计模式》是作者最近出版的新书,拜托多多关注!

本书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。

对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。


觉得本文有帮助?请分享给更多人。

关注微信公众号【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!

判断exception类型_C++核心准则T.44:使用函数模板推断类模板参数类型(如果可能)...相关推荐

  1. 类模板,多种类型的类模板,自定义类模板,类模板的默认类型,数组的模板实现,友元和类模板,友元函数,类模板与静态变量,类模板与普通类之间互相继承,类模板作为模板参数,类嵌套,类模板嵌套,类包装器

     1.第一个最简单的类模板案例 #include "mainwindow.h" #include <QApplication> #include <QPush ...

  2. C++模板(函数模板,类模板)的基本使用与非类型模板参数与模板的特化

    C++模板 模板初阶 泛型编程 函数模板 函数模板概念 函数模板格式 函数模板的原理 函数模板的实例化 隐式实例化 显式实例化:在函数名后的<>中指定模板参数的实际类型 模板参数的匹配原则 ...

  3. modbus软件开发实战指南_C++核心准则?GSL:指南支持库

    GSL: Guidelines support library GSL:指南支持库 The GSL is a small library of facilities designed to suppo ...

  4. c++ h cpp文件如何关联_C++核心准则SF.5: .cpp文件必须包含定义它接口的.h文件

    SF.5: A .cpp file must include the .h file(s) that defines its interface SF.5: .cpp文件必须包含定义它接口的.h文件 ...

  5. template标签_C++核心准则T.65:使用标签分发提供函数的不同实现

    T.65: Use tag dispatch to provide alternative implementations of a function T.65:使用标签分发提供函数的不同实现 Rea ...

  6. unique函数_C++核心准则C.35:基类的析构函数必须满足的条件

    C.35: A base class destructor should be either public and virtual, or protected and nonvirtual 基类的析构 ...

  7. blob转file对象_C++核心准则C.41:构造函数生成的对象应该被完全初始化

    C.41: A constructor should create a fully initialized object C.41构造函数生成的对象应该被完全初始化 Reason(原因) A cons ...

  8. java 成员变量声明顺序_C++核心准则讨论:按照成员声明的顺序定义和初始化成员变量...

    Discussion: Define and initialize member variables in the order of member declaration 讨论:按照成员声明的顺序定义 ...

  9. 无符号数的减法_C++核心准则ES.107:不要使用无符号数下标,使用gsl::index更好

    ES.107: Don't use unsigned for subscripts, prefer gsl::index ES.107:不要使用无符号数下标,使用gsl::index更好 Reason ...

最新文章

  1. CDH 5 Maven Repository
  2. 使用MyBatis的Generator自动创建实体类和dao的接口与xml
  3. SimpleDraw-Windows Phone7上的应用
  4. 弱引用使用场景桌面_面试|再次讲解Threadlocal使用及其内存溢出
  5. Ansible 快速入门
  6. MVDR波束形成MATLAB实现
  7. location 拦截所有_电脑广告拦截软件 Adguard Premium
  8. java 调用 Oracle 存储过程
  9. 解决:Gitlab上出现“You won‘t be able to pull or push project code via SSH until you add an SSH key to you
  10. Slimer软工课设日报-2016年7月1日
  11. memcached mysql 性能测试_InnoDB memcached插件 vs 原生memcached对比性能测试
  12. Docker系列(五)实战:在容器中部署静态网站
  13. Array 对象-sort()
  14. Splunk 模式的中国践行者——日志易让日志分析更容易
  15. 使用three.js的着色器通道一之渲染地球模型
  16. 【面试题】为什么需要 public static void main (String[ ] args) 这个方法?
  17. BZOJ 1216 优先队列
  18. [Thoughts]敏捷方法大全
  19. 直方图规定化(直方图匹配)
  20. 走进GBase 8s之高可用功能(二)HAC灾备高可用

热门文章

  1. Python使用pandas保存csv文件:如果文件存在则只添加内容(append),如果无表则同时写入表头和内容(write)
  2. Python多分类问题下,micro-PR计算以及macro-PR计算
  3. R语言关系操作符:>、<=、!=、>=、==、
  4. 分子特征数据库R包msigdb
  5. ISLR_StatisticalLearning
  6. FASTQ! BAM! VCF
  7. 实验三:XML模型(一)
  8. 【跨域报错解决方案】Access to XMLHttpRequest at ‘http://xxx.com/xxx‘ from origin ‘null‘ has been blocked by
  9. 深度学习:知识回收(Lecture3+4 PLA+Decision Tree)
  10. 人群密度估计--Generating High-Quality Crowd Density Maps using Contextual Pyramid CNNs