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

发布于 2024-07-13 17:47:09 字数 335 浏览 5 评论 0 原文

C++0x 引入了概念,基本上可以让您定义类型的类型。 它指定类型所需的属性

C# 允许您使用“where”子句。

它们之间有语义差异吗?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女皇必胜 2024-07-20 17:47:09

需要记住的一件事是 C++ 模板和 C# 泛型并不完全相同。 有关这些差异的更多详细信息,请参阅此答案

从您链接到解释 C++0x 概念的页面来看,听起来这个想法是在 C++ 中您希望能够指定模板类型实现某些属性。 在 C# 中,约束比这更进一步,它强制泛型类型属于该约束。 例如,以下 C# 代码:

public GenericList<T> where T : IDisposable

表示用于代替 T 的任何类型必须实现 IDisposable 接口。 同样,以下代码:

public abstract class ABC {}
public class XYZ : ABC {}

public GenericList<T> where T : ABC

表示用于代替 T 的任何类型都必须是 ABC 类型或从 ABC 派生。

C++0x 的概念思想只是说用来代替 T 的类型必须具有与 ABC(或 IDisposable)定义的相同的属性,而不是它必须是该类型。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文