The Wayback Machine - https://web.archive.org/web/20180112033912/http://ja.cppreference.com:80/w/cpp/language/class_template
名前空間
変種
操作

Class template

提供: cppreference.com
< cpp‎ | language

 
 
C++言語
一般的なトピック
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
フロー制御
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
条件付き実行文
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
繰り返し文
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
文をジャンプします
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
機能します
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
関数の宣言
ラムダ関数の宣言
関数テンプレート
の歴史。インライン指定
例外仕様(廃止予定)
noexcept指定子(C++11)
例外
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
名前空間
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
タイプ
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier(C++11)
指定子
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
CV指定
貯蔵期間指定
constexprの指定子(C++11)
自動指定(C++11)
alignas指定子(C++11)
初期化
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
リテラル
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
代替表現
ユーティリティ
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
タイプ
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
型の別名宣言(C++11)
属性(C++11)
キャストします
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
暗黙の型変換
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
Cスタイルキャストと機能
メモリの割り当て
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラス
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラス固有の機能特性
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
特殊なメンバ関数
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
テンプレート
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラステンプレート
関数テンプレート
テンプレートの特殊化
パラメーターパック(C++11)
その他
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
インラインアセンブリ
 

A class template defines a family of classes.

目次

[編集]構文

template<parameter-list>declaration

[編集]説明

declaration defines or declares a class (including struct and union), a member class or member enumeration type, a function or member function, a static data member of a class template, or a type alias. It may also define a テンプレートの特殊化. This page focuses on class templates.

parameter-list is a non-empty comma-separated list of the template parameters, each of which is either non-type parameter, a type parameter, a template parameter, or a パラメーターパック of any of those. This page focuses on the parameters that are not parameter packs.

[編集]Non-type template parameter

typename (1)
typename= default (2)
type...name (3) (C++11およびそれ以降)
1) A non-type template parameter with an optional name
2) A non-type template parameter with an optional name and a default value
3) A non-type template パラメーターパック with an optional name

type is one of the following types (optionally cv-qualified, the qualifiers are ignored)

  • integral type
  • enumeration
  • pointer to object or to function
  • lvalue reference to object or to function
  • pointer to member object or to member function
  • std::nullptr_t(C++11およびそれ以降)

Array and function types may be written in a template declaration, but they are automatically replaced by pointer to data and pointer to function as appropriate.

When the name of a non-type template parameter is used in an expression within the body of the class template, it is an unmodifiable prvalue unless its type was an lvalue reference type.

[編集]Type template parameter

typenamename (1)
classname (2)
typename|classname= default (3)
typename|class...name (4) (C++11およびそれ以降)
1) A type template parameter with an optional name
2) Exactly the same as 1)
3) A type template parameter with an optional name and a default
5) A type template パラメーターパック with an optional name

[編集]Template template parameter

template<parameter-list>name (1)
template<parameter-list>name=default (2)
template<parameter-list>...name (3) (C++11およびそれ以降)
1) A template template parameter with an optional name
2) A template template parameter with an optional name and a default
3) A template template パラメーターパック with an optional name

[編集]Class template instantiation

A class template by itself is not a type, or an object, or any other entity. No code is generated from a source file that contains only template definitions. In order for any code to appear, a template must be instantiated: the template arguments must be provided so that the compiler can generate an actual class (or function, from a function template).

[編集]Explicit instantiation

templateclassname<argument-list> ; (1)
externtemplateclassname<argument-list> ; (2) (C++11およびそれ以降)
1) Explicit instantiation definition
2) Explicit instantiation declaration

An explicit instantiation definition forces instantiation of the class, struct, or union they refer to. It may appear in the program anywhere after the template definition, and for a given argument-list, is only allowed to appear once in the program.

An explicit instantiation declaration (an extern template) prevents implicit instantiations: the code that would otherwise cause an implicit instantiation has to use the explicit instantiation definition provided somewhere else in the program.

[編集]Implicit instantiation

When code refers to a template in context that requires a completely defined type, or when the completeness of the type affects the code, and this particular type has not been explicitly instantiated, implicit instantiation occurs. For example, when an object of this type is constructed, but not when a pointer to this type is constructed.

This applies to the members of the class template: unless the member is used in the program, it is not instantiated, and does not require a definition.

template<class T>struct Z {
    void f(){}
    void g();// never defined
};// template definition
templatestruct Z<double>;// explicit instantiation of Z<double>
Z<int> a;// implicit instantiation of Z<int>
Z<char>* p;// nothing is instantiated here
p->f();// implicit instantiation of Z<char> and Z<char>::f() occurs here.
// Z<char>::g() is never needed and never instantiated: it does not have to be defined

[編集]Non-type template parameters

The following limitations apply when instantiating class templates that have non-type template parameters:

  • For integral and arithmetic types, the template argument provided during instantiation must be a constant expression.
  • For pointers to objects, the template arguments have to designate the address of an object with static storage duration and a linkage (either internal or external), or a constant expression that evaluates to the appropriate null pointer value.
  • For pointers to functions, the valid arguments are pointers to functions with linkage (or constant expressions that evaluate to null pointer values).
  • For lvalue reference parameters, the argument provided at instantiation cannot be a temporary, an unnamed lvalue, or a named lvalue with no linkage.
  • For pointers to members, the argument has to be a pointer to member expressed as &Class::Member or a constant expression that evaluates to null pointer value.

In particular, this implies that string literals, addresses of array elements, and addresses of non-static members cannot be used as template arguments to instantiate templates whose corresponding non-type template parameters are pointers to data.

[編集]

template<typename T>struct S {template<typename U>void foo(){}};   template<typename T>void bar(){ S<T>s; s.foo<T>();// error: < parsed as less than operator s.template foo<T>();// OK}


[編集]Non-type template parameters

#include <iostream>   // simple non-type template parametertemplate<int N>struct S {int a[N];};   template<constchar*>struct S2 {};   // complicated non-type exampletemplate<char c, // integral typeint(&ra)[5], // lvalue reference to object (of array type)int(*pf)(int), // pointer to functionint(S<10>::*a)[10]// pointer to member object (of type int[10])>struct Complicated {// calls the function selected at compile time// and stores the result in the array selected at compile timevoid foo(char base){ ra[4]= pf(c - base);}};   // S2<"fail"> s2; // Error: string literal cannot be usedchar okay[]="okay";// static object with linkage// S2< &okay[0] > s2; // Error: array element has no linkage S2<okay> s2;// works   int a[5];int f(int n){return n;}int main(){ S<10> s;// s.a is an array of 10 int s.a[9]=4;   Complicated<'2', a, f, &S<10>::a> c; c.foo('0');   std::cout<< s.a[9]<< a[4]<<'\n';}

出力:

42

[編集]参照

close