ジェネリック関数 (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
(省略可能)追加の宣言型情報。 属性と属性クラスについては、属性を参照してください。

constraint-clause-list
この省略可能なリストでは、型引数として使用できる型の制限を指定します。 ジェネリック型パラメーターの制約 (C++/CLI)指定された形式を受け取ります。

function-definition
メソッドまたはスタンドアロン関数の定義。 関数に修飾子がない virtual 可能性があります。これは、仮想メソッドがジェネリックではない可能性があるため、許可されません。 関数の本体は、ジェネリック型パラメーター識別子を参照できます。 関数は、関数である operator 可能性があります。

generic-id
ジェネリック関数のインスタンスを呼び出すときに、それを実装するために使用する型を generic-argument-list指定します。 このリストは 、に generic-parameter-list対応し、オプション constraint-clause-listの制約を満たす必要があります。

generic-name
ジェネリック関数は、名前として持 identifier っているか、関数である可能性 operator があります。

解説

ジェネリック関数は、1 つ以上のジェネリック型パラメーターで宣言された関数です。 class または struct のメソッド、またはスタンドアロン関数が可能です。 単一のジェネリック宣言は、ジェネリック型パラメーターに置換される実際の型だけが異なる関数ファミリを暗黙的に宣言します。

class または struct のコンストラクターは、ジェネリック型パラメーターで宣言できません。

呼び出されると、ジェネリック型パラメーターは、実際の型に置換されます。 実際の型は、関数テンプレート呼び出しと同様の構文を使用して、山かっこで明示的に指定できます。 型パラメーターなしで呼び出すと、コンパイラは、関数呼び出しで提供されたパラメーターから実際の型を推測しようとします。 使用されるパラメーターから目的の型引数を推定できない場合、コンパイラはエラーを報告します。

必要条件

コンパイラ オプション: /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) {}
};

次の例では、ジェネリック関数を使用して、配列内の最初の要素を検索します。 MyClass を宣言し、基底クラス MyBaseClass から継承します。 MyClass には、基底クラス内の別のジェネリック関数 MyBaseClassFunction を呼び出すジェネリック関数 MyFunction が含まれます。 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 でのコンポーネント拡張
ジェネリック