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.
需要记住的一件事是 C++ 模板和 C# 泛型并不完全相同。 有关这些差异的更多详细信息,请参阅此答案。
从您链接到解释 C++0x 概念的页面来看,听起来这个想法是在 C++ 中您希望能够指定模板类型实现某些属性。 在 C# 中,约束比这更进一步,它强制泛型类型属于该约束。 例如,以下 C# 代码:
表示用于代替 T 的任何类型必须实现 IDisposable 接口。 同样,以下代码:
表示用于代替 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:
says that any type used in place of T must implement the IDisposable interface. Likewise, the following code:
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.