C++核心准则T.12:声明局部变量类型时,概念比auto更好

时间:2022-07-24
本文章向大家介绍C++核心准则T.12:声明局部变量类型时,概念比auto更好,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

T.12: Prefer concept names over auto for local variables

T.12:声明局部变量类型时,概念比auto更好

Reason(原因)

auto is the weakest concept. Concept names convey more meaning than just auto.

auto是最弱的概念。相比只用auto,概念名可以传递更多信息。

Example (using TS concepts)

示例(使用TS概念)

vector<string> v{ "abc", "xyz" };
auto& x = v.front();     // bad
String& s = v.front();   // good (String is a GSL concept)
Enforcement(实施建议)
  • ???

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#t11-whenever-possible-use-standard-concepts