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.
C++0x was the working name for the new standard for C++, adding many language features that I'll cover in this series on C++11. In September 2011, C++0x was officially published as the new C++11 standard, and many compilers now provide support for some of the core C++11 features.
Concepts are a revolutionary approach for writing templates! They allow you to put constraints on template parameters that improve the readability of code, speed up compilation time, and give better error messages. Read on and learn how to use them in your code!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With