C# generics vs C++ templates - need a clarification about constraints

前端 未结 5 1018
萌比男神i
萌比男神i 2021-02-14 05:15

Duplicate

What are the differences between Generics in C# and Java… and Templates in C++?


Hi all,

I am experienced C++ p

5条回答
  •  死守一世寂寞
    2021-02-14 06:06

    C# generics are totally different from C++.

    In C#, the compiler basically compiles one class definition for all object types, and a class definition per value type.

    In C++, each type gets its own class definitions.

    The constraints are just for the compiler so you can infer stuff from other locations.

    I'd recommend looking at the Action, Func and Predicate delegates and the associated IEnumerable extension methods. Use lambda functions with those and you'll see what the constraints doing.

提交回复
热议问题