function - 泛型函数,类和 “where”关键字

标签 function c++-cli where-clause generics

如何使用“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;
   }

最佳答案

  • 在您的示例中,wheregeneric <class T>ref class Stack之间。
  • 来自the C++/CLI documentation:

    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/

    相关文章:

    python-3.x - python : variables in a function with dot preceded by the function name

    c++ - 对 Class::Class/Function 的 undefined reference (OOP 初学者)

    c++/cli dll 包装器,用于在 LabView 中使用的原生 c++

    mysql - Laravel Eloquent ORMWhere 语句在默认的created_at时间戳上进行日期时间戳比较

    powershell - 混合 Where-Object/ForEach-Object 语法

    javascript - 如何使 Vorpal 输出命令的描述

    javascript - 如何将变量传递给函数并为其设置新值?

    c++ - LNK2022 元数据操作 : Inconsistent layout information in duplicated types

    c++-cli - 智能感知 : "#using" requires C++/CLI to be enabled

    php - SQL, "WHERE IN"返回自动排序结果