简体   繁体   English

c ++ 0x概念和c#约束之间有什么区别?

[英]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. C ++ 0x引入了概念 ,基本上可以定义一种类型。 It specifies the properties required of a type . 它指定了类型所需的属性

C# let you specify constraints of a generic with the " where " clause. C#允许您使用“ where ”子句指定泛型的约束

Is there any semantic difference between them? 它们之间是否存在语义差异?

Thank you. 谢谢。

One thing to keep in mind is that C++ templates and C# generics are not exactly the same. 要记住的一件事是C ++模板和C#泛型不完全相同。 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. 从您链接到解释C ++ 0x概念的页面,听起来好像是在C ++中您希望能够指定模板类型实现某些属性。 In C#, the constraint goes further than that and forces the generic type to be "of" that constraint. 在C#中,约束比这更进一步,并强制泛型类型为“约束”。 For example, the following C# code: 例如,以下C#代码:

public GenericList<T> where T : IDisposable

says that any type used in place of T must implement the IDisposable interface. 表示用于代替T的任何类型都必须实现IDisposable接口。 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. 表示用于代替T的任何类型必须是ABC类型或从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. C ++ 0x概念只表示用于代替T的类型必须具有与ABC(或IDisposable)定义的相同属性,而不是必须属于该类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM