什么是概念? [英] What are concepts?

查看:180
本文介绍了什么是概念?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说过所有这些新的(on /。)关于C ++ 0x不再有概念,但我不知道它们是什么?有人可以向我解释吗?

I've heard all this new (on /.) about C++0x not having concepts anymore, but I have no idea what they are? Can someone explain to me?

推荐答案

概念是一个通用的编程特性,允许有人编写模板代码来指定类型参数需要满足。

Concepts are a generic programming feature which allow someone writing templated code to specify requirements which the type parameters need to meet.

例如,一些集合类型需要集合的type参数来定义<运算符。因此,程序员可以定义一个称为LessThanComparable的概念,告诉编译器模板类的类型参数需要具有operator<定义。如果模板用户然后试图使用不具有LessThanComparable概念的类型(即,不具有operator< function)来实例化模板,则编译器可以发出简单的错误消息,而不是与模板化代码相关联的洪流的错误消息。编译器也可以利用概念提供的额外信息来生成更有效的代码。

For example, some collection types need for the type parameter for the collection to define the < operator. So the programmer might define a concept called LessThanComparable which tells the compiler that the type parameter to the templated class needs to have operator< defined. If the template user then tries to instantiate the template using a type that does not have the LessThanComparable concept (i.e. does not have an operator< function) the compiler can emit a simple error message rather than the torrent of error messages associated with templated code. The compiler may also be able to take advantage of the extra information provided by concepts to generate more efficient code.

这是一个过于简单的例子,但我认为它给了你

This is somewhot of an oversimplication, but I think it gives you the general idea behind concepts.

如果你想尝试一些概念的功能,请查看Boost.Concept 检查库。我不认为它提供了将在标准中的全部功能,但它是一个好的开始的地方。

If you want to try out some of the capabilities of concepts, take a look at the Boost.Concept Check library. I don't think it provides the full range of capabilities that were going to be in the standard, but it's a good place to start.

您可能还想看看在 ConceptC ++ ,有一个很好的概念定义。

You may also want to look at ConceptC++, there's a good definition of concepts there.

这篇关于什么是概念?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆