温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++怎么为模板参数定义概念

发布时间:2021-11-24 11:23:22 来源:亿速云 阅读:137 作者:iii 栏目:大数据

这篇文章主要讲解了“C++怎么为模板参数定义概念”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C++怎么为模板参数定义概念”吧!

T.10:为所有的模板参数定义概念

Reason(原因)

Correctness and readability. The assumed meaning (syntax and semantics) of a template argument is fundamental to the interface of a template. A concept dramatically improves documentation and error handling for the template. Specifying concepts for template arguments is a powerful design tool.

正确性和可读性。一个模板参数的假定含义(语法和语义)是模板接口的基础。概念大幅度改善了模板的文档化和错误处理。为模板参数定义概念是一个强有力的设计工具。

Example(实例)

template<typename Iter, typename Val>
//    requires Input_iterator<Iter>
//             && Equality_comparable<Value_type<Iter>, Val>
Iter find(Iter b, Iter e, Val v)
{
   // ...
}

or equivalently and more succinctly:

或者使用下面功能等价但更简洁的方式:

template<Input_iterator Iter, typename Val>
//    requires Equality_comparable<Value_type<Iter>, Val>
Iter find(Iter b, Iter e, Val v)
{
   // ...
}
Note(注意)

"Concepts" are defined in an ISO Technical Specification: concepts. A draft of a set of standard-library concepts can be found in another ISO TS: ranges Concepts are supported in GCC 6.1 and later. Consequently, we comment out uses of concepts in examples; that is, we use them as formalized comments only. If you use GCC 6.1 or later, you can uncomment them:

“概念”被ISO技术规格:concepts定义。一套标准库concepts的初步版本可以在另一个ISO技术规格:ranges中找到。GCC6.1以后都支持concepts。因此我们在实例代码中注释掉使用concepts的部分;也就是说我们只是将它们用作标准的注释。如果你使用GCC6.1之后的版本,可以打开注释。

template<typename Iter, typename Val>

    requires Input_iterator<Iter>
          && Equality_comparable<Value_type<Iter>, Val>
Iter find(Iter b, Iter e, Val v)
{
   // ...
}
Note(注意)

Plain typename (or auto) is the least constraining concept. It should be used only rarely when nothing more than "it's a type" can be assumed. This is typically only needed when (as part of template metaprogramming code) we manipulate pure expression trees, postponing type checking.

直接的类型名(或auto)是最小约束的概念。它应该被极少使用,仅限于表现“它是一个类型”。这通常只在我们操作纯表达式树,延迟类型检查时有(作为模板元编程的一部分)存在的必要。

感谢各位的阅读,以上就是“C++怎么为模板参数定义概念”的内容了,经过本文的学习后,相信大家对C++怎么为模板参数定义概念这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++
AI