C++核心准则T.122:使用模板在编译时计算类型

时间:2022-07-26
本文章向大家介绍C++核心准则T.122:使用模板在编译时计算类型,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

T.122: Use templates (usually template aliases) to compute types at compile time

T.122:使用模板(通常是模板别名)在编译时计算类型

Reason(原因)

Template metaprogramming is the only directly supported and half-way principled way of generating types at compile time.

模板元编程是语言直接支持的唯一一种差不多可以说是符合规则的方式的编译时生成类型的方式。

Note(注意)

"Traits" techniques are mostly replaced by template aliases to compute types and constexpr functions to compute values.

“特征”技术用于计算类型时基本上已经模板别名所代替,用于求值时则被常量表达式函数所代替。

Example(示例)

 big object / small object optimization

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#t122-use-templates-usually-template-aliases-to-compute-types-at-compile-time