What are concepts?

前端 未结 1 1503
隐瞒了意图╮
隐瞒了意图╮ 2021-02-04 01:22

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条回答
  • 2021-02-04 01:45

    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.

    0 讨论(0)
提交回复
热议问题