简体   繁体   中英

What is the difference between c++0x concepts and c# constraints?

C++0x introduces concepts , that let you define, basically, a type of a type. It specifies the properties required of a type .

C# let you specify constraints of a generic with the " where " clause.

Is there any semantic difference between them?

Thank you.

One thing to keep in mind is that C++ templates and C# generics are not exactly the same. See this answer for more details on those differences.

From the page you linked to explaining C++0x concepts, it sounds like the idea is that in C++ you want to be able to specify that the template type implements certain properties. In C#, the constraint goes further than that and forces the generic type to be "of" that constraint. For example, the following C# code:

public GenericList<T> where T : IDisposable

says that any type used in place of T must implement the IDisposable interface. Likewise, the following code:

public abstract class ABC {}
public class XYZ : ABC {}

public GenericList<T> where T : ABC

says that any type used in place of T must be of type ABC or derived from ABC.

The C++0x concept idea says only that the type used in place of T must have the same properties as defined by ABC (or IDisposable) not that it must be of that type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM