Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top