What are concepts?

北城余情 提交于 2019-12-18 11:45:01

问题


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?


回答1:


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

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 something of an oversimplication, but I think it gives you the general idea behind concepts.

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.

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




回答2:


Here is an article that I think would help:

http://www.devx.com/SpecialReports/Article/38864

The decision to remove them has been discussed several times here at SO as well. These might prove interesting:

c0x No longer has concepts

Concepts compared to Interfaces

Hypothetical discussion of concepts



来源:https://stackoverflow.com/questions/1252841/what-are-concepts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!