English 中文(简体)
为什么在我们已经有班级和接口时设想了概念(基因规划)?
原标题:Why were concepts (generic programming) conceived when we already had classes and interfaces?

<Also on programmers.stack Exchange.com:

I understand that STL concepts had to exist, and that it would be silly to call them "classes" or "interfaces" when in fact they re only documented (human) concepts and couldn t be translated into C++ code at the time, but when given the opportunity to extend the language to accomodate concepts, why didn t they simply modify the capabilities of classes and/or introduced interfaces?

Isn t a concept very similar to an interface (100% abstract class with no data)? By looking at it, it seems to me interfaces only lack support for axioms, but maybe axioms could be introduced into C++ s interfaces (considering an hypothetical adoption of interfaces in C++ to take over concepts), couldn t them? I think even auto concepts could easily be added to such a C++ interface (auto interface LessThanComparable, anyone?).

这一概念是否类似于适应模式? 如果所有方法都行不通,适应者基本上没有时间汇编;汇编者只是将电话换成在线版本,在运行期间直接将目标对象称作接口。

I ve heard of something called Static Object-Oriented Programming, which essentially means effectively reusing the concepts of object-orientation in generic programming, thus permitting usage of most of OOP s power without incurring execution overhead. Why wasn t this idea further considered?

I hope this is clear enough. I can rewrite this if you think I was not; just let me know.

问题回答

There is a big difference between OOP and Generic Programming, Predestination.

In OOP, when you design the class, you had the interfaces you think will be useful. And it s done.

In Generic Programming, on the other hand, as long as the class conforms to a given set of requirements (mainly methods, but also inner constants or types), then it fits the bill and may be used. The Concept proposal is about formalizing this, so that detection may occur directly when checking the method signature, rather than when instantiating the method body. It also makes checking template methods more easily, since some methods can be rejected without any instantiation if the concepts do not match.

The advantage of Concepts is that you do not suffer from Predestination, you can pick a class from Library1, pick a method from Library2, and if it fits, you re gold (if it does not, you may be able to use a concept map). In OO, you are required to write a full-fledged Adapter, every time.

你是正确无误的。 差别主要在于有约束力的时间(以及概念仍然有静态的派遣,而不是像接口一样有动态的派遣)。 概念更加开放,便于使用。

Classes are a form of named conformance. You indicate that class Foo conforms with interface I by inheriting from I.

Concepts are a form of structural and/or runtime conformance. A class Foo does not need to state up front which concepts it conforms to.

The result is that named conformance reduces the ability to reuse classes in places that were not expected up front, even though they would be usable.

The concepts are in fact not part of C++, they are just concepts! In C++ there is no way to "define a concept". All you have is, templates and classes (STL being all template classes, as the name says: S tandard T emplate L ibrary).

如果你指C++0x,而不是C++(在这种情况下,我建议你改变标签),请在此读:

http://en.wikipedia.org/wiki/Concepts_(C++)

部分 我将给你写信。

In the pending C++0x revision of the C++ programming language, concepts and the related notion of axioms were a proposed extension to C++ s template system, designed to improve compiler diagnostics and to allow programmers to codify in the program some formal properties of templates that they write. Incorporating these limited formal specifications into the program (in addition to improving code clarity) can guide some compiler optimizations, and can potentially help improve program reliability through the use of formal verification tools to check that the implementation and specification actually match.

2009年7月,C++0x委员会决定从标准草案中删除概念,因为这些概念被视为C++0x的“未准备好”。

引入概念的主要动力是,以提高编造错误信息的质量。

因此,正如你所看到的那样,不存在取代接口等的概念,它们只是帮助汇编者更好地优化和产生更好的错误。

虽然我同意所有已张贴的答复,但似乎忽略了一个业绩点。 与接口不同的是,对概念进行汇编,因此不需要虚拟功能电话。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?