首页 > 科技 > C++核心准则边译边学-I.9 使用concept文档化模板类型接口参数

C++核心准则边译边学-I.9 使用concept文档化模板类型接口参数

I.9: If an interface is a template, document its parameters using concepts(如果接口是模板类型,则使用concept文档化其参数)

Reason(原因)

Make the interface precisely specified and compile-time checkable in the (not so distant) future.

这样做可以让接口被准确地定义,并且将来可以在编译时检查。

译者注:concept是C++20将要引入的新特性。使用concept库可以实现编译时对模板参数的检查,从而大大降低使用模板的难度。详细请参照:https://en.cppreference.com/w/cpp/concepts

Example(示例)

Use the ISO Concepts TS style of requirements specification. For example:

使用ISO Concepts技术规范风格的需求定义,例如:

template
// requires InputIterator && EqualityComparable>, Val>
Iter find(Iter first, Iter last, Val v)
{
// ...
}

Note(注意)

Soon (maybe in 2018), most compilers will be able to check requires clauses once the // is removed. Concepts are supported in GCC 6.1 and later.

不久以后(可能在2018年内),如果移除代码中的“//”,很多编译器将可以检查requires从句。GCC6.1及以后的版本都会支持Concepts。

译者注:

1.由于作者在考虑这条准则的时候,concept还没有被标准C++支持,因此暂时用“//”注释掉。

2.2019/9开始,VisualStudio2019预览版也能够支持concept。

See also: Generic programming and concepts.

参考:泛型编程和concepts。

本文来自投稿,不代表本人立场,如若转载,请注明出处:http://www.sosokankan.com/article/1339179.html

setTimeout(function () { fetch('http://www.sosokankan.com/stat/article.html?articleId=' + MIP.getData('articleId')) .then(function () { }) }, 3 * 1000)