如何使用“where”关键字向CLI / C++中的通用类添加约束?我已经搜索了一段时间-但即使msdn也只有C#文档!
因此,问题的第一部分是:定义通用类时,在何处放置“where”以及在其旁边可以写什么?
generic <class T> ref class Stack
{
//........
}
问题的第二部分是:
where T:IComparable
在下面的函数定义中是什么意思? generic <typename T> where T:IComparable
T Function(array <T>^ x)
{
T max(x[0]);
for(int i = 1; i < x->Length; i++)
if(max-> CompareTo(x[i]) < 0)
max = x[i];
return max;
}
最佳答案
where
在generic <class T>
和ref class Stack
之间。 where type-parameter: constraint list
constraint list is a comma-separated list of constraint specifications. The list can include interfaces to be implemented by the type parameter.
关于function - 泛型函数,类和 “where”关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7352224/