Share via


泛型函数 (C++/CLI)

泛型函数是指使用类型参数声明的函数。 在函数调用后,使用的是实际类型,而不是类型参数。

所有平台

备注

此功能不适用于所有平台。

Windows 运行时

备注

Windows 运行时不支持此功能。

要求

不适用。

公共语言运行时

泛型函数是指使用泛型类型参数声明的函数。 在函数调用后,使用的是实际类型,而不是类型参数。

语法

generic-declaration
generic<generic-parameter-list>constraint-clause-listoptfunction-definition

generic-parameter-list
generic-parameter
generic-parameter-list , generic-parameter

generic-parameter
attributesoptclassidentifier
attributesopttypenameidentifier

constraint-clause-list
constraint-clause-listoptconstraint-clause

constraint-clause
where identifier : constraint-item-list

constraint-item-list
constraint-item
constraint-item-list , constraint-item

constraint-item
type-id
ref class
ref struct
value class
value struct
gcnew ( )

generic-id
generic-name < generic-argument-list >

generic-name
identifier
operator-function-id

generic-argument-list
generic-argument
generic-argument-list , generic-argument

generic-argument
type-id

参数

generic-parameter-list
可选属性化泛型类型参数标识符的逗号分隔列表。

attributes
(可选)额外声明信息。 若要详细了解属性和属性类,请参阅 attribute

constraint-clause-list
此可选列表指定了对可以用作类型参数的类型施加的限制。 采用泛型类型参数的约束 (C++/CLI) 中指定的形式。

function-definition
方法或独立函数的定义。 该函数可能没有 virtual 修饰符,禁止使用是因为虚拟方法可能不是泛型。 函数主体可能会引用泛型类型参数标识符。 该函数可能是 operator 函数。

generic-id
调用泛型函数的实例时,指定用于在 generic-argument-list 中实现它的类型。 此列表对应于 generic-parameter-list,并且必须满足可选 constraint-clause-list 的约束。

generic-name
泛型函数可能用 identifier 作为其名称,或者可能是 operator 函数。

备注

泛型函数是指使用一个或多个泛型类型参数声明的函数。 它们可以是 classstruct 中的方法,也可以是独立函数。 一个泛型声明隐式声明一系列函数,这些函数的唯一区别是,使用不同的实际类型来替换泛型类型参数。

classstruct 构造函数无法使用泛型类型参数进行声明。

在函数调用后,实际类型替换泛型类型参数。 可使用类似于函数模板调用的语法,在尖括号中显式指定实际类型。 如果在调用函数时没有类型参数,编译器会尝试根据函数调用中提供的参数推导实际类型。 如果无法根据所用参数推导预期类型参数,编译器会报告错误。

要求

编译器选项:/clr

示例

下面的代码示例展示了泛型函数。

// generics_generic_function_1.cpp
// compile with: /clr
generic <typename ItemType>
void G(int i) {}

ref struct A {
   generic <typename ItemType>
   void G(ItemType) {}

   generic <typename ItemType>
   static void H(int i) {}
};

int main() {
   A myObject;

   // generic function call
   myObject.G<int>(10);

   // generic function call with type parameters deduced
   myObject.G(10);

   // static generic function call
   A::H<int>(10);

   // global generic function call
   G<int>(10);
}

泛型函数可以根据签名或 arity(函数中类型参数的数量)进行重载。 此外,泛型函数还可以重载有同名的非泛型函数,只要这些函数的一些类型参数不同即可。 例如,可以重载下面的函数:

// generics_generic_function_2.cpp
// compile with: /clr /c
ref struct MyClass {
   void MyMythod(int i) {}

   generic <class T>
   void MyMythod(int i) {}

   generic <class T, class V>
   void MyMythod(int i) {}
};

下面的示例使用泛型函数来查找数组中的第一个元素。 它声明继承自基类 MyBaseClassMyClassMyClass 包含泛型函数 MyFunction,此函数调用基类中的另一个泛型函数 MyBaseClassFunction。 在 main 中,泛型函数 MyFunction 是通过使用不同的类型参数进行调用。

// generics_generic_function_3.cpp
// compile with: /clr
using namespace System;

ref class MyBaseClass {
protected:
   generic <class ItemType>
   ItemType MyBaseClassFunction(ItemType item) {
      return item;
   }
};

ref class MyClass: public MyBaseClass {
public:
   generic <class ItemType>
   ItemType MyFunction(ItemType item) {
      return MyBaseClass::MyBaseClassFunction<ItemType>(item);
   }
};

int main() {
   MyClass^ myObj = gcnew MyClass();

   // Call MyFunction using an int.
   Console::WriteLine("My function returned an int: {0}",
                           myObj->MyFunction<int>(2003));

   // Call MyFunction using a string.
   Console::WriteLine("My function returned a string: {0}",
   myObj->MyFunction<String^>("Hello generic functions!"));
}
My function returned an int: 2003
My function returned a string: Hello generic functions!

另请参阅

.NET 和 UWP 的组件扩展
泛型