views:

265

answers:

1

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.

+6  A: 

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.

Scott Dorman
if it flies like a duck and quacks like a duck then it must be a duck? :-)
Zen
Unless it's a *concept* of a duck. There does seem to be a subtle difference in that C# enforces the fact that the type must be of the constraint while a concept simply says it must have those properties but doesn't have to be of that type.
Scott Dorman