Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 635
Zen Avatar asked Jan 31 '09 00:01

Zen


People also ask

What does this STD C ++ 0x mean?

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.

What are C ++ 20 concepts?

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!


1 Answers

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.

like image 107
Scott Dorman Avatar answered Oct 05 '22 06:10

Scott Dorman