什么是概念?

发布于 2024-07-30 09:02:52 字数 65 浏览 4 评论 0原文

我听说过所有关于 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃气十足 2024-08-06 09:02:52

概念是一种通用编程功能,允许编写模板代码的人指定类型参数需要满足的要求。

例如,某些集合类型需要集合的类型参数来定义 << 操作员。 因此,程序员可能会定义一个名为 LessThanComparable 的概念,它告诉编译器模板化类的类型参数需要具有运算符<; 定义的。 如果模板用户随后尝试使用不具有 LessThanComparable 概念(即不具有运算符<函数)的类型来实例化模板,则编译器可以发出简单的错误消息,而不是与模板化代码关联的大量错误消息。 编译器还可以利用概念提供的额外信息来生成更有效的代码。

这有点过于简单化,但我认为它为您提供了概念背后的总体思路。

如果您想尝试概念的一些功能,请查看 Boost.Concept 检查库。 我不认为它提供了标准中的全部功能,但它是一个很好的起点。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文