[ Web Proxy ]
URL:
Viewing: https://fr.cppreference.com/cpp/language/class_template [Back]  [Original]

Class template cppreference.com
cppreference.com
Espaces de noms
Variantes

Class template

De cppreference.com

<metanoindex/>

 
 
Langage C++
Sujets gnraux
Contrle de flux
Instructions conditionnelles
Instructions d'itration
Instructions de saut
Fonctions
dclaration de fonction
expression lambda
fonction gnrique
spcificateur inline
spcification d'exception (obsolte)
spcificateur noexcept (C++11)
Exceptions
Espaces de noms
Types
spcificateur decltype (C++11)
Qualificatifs
qualificatifs const et volatile
qualificatifs de stockage
qualificatif constexpr (C++11)
qualificatif auto (C++11)
qualificatif alignas (C++11)
Initialisation
Littraux
Expressions
oprateurs alternatifs
Utilitaires
Types
dclaration typedef
dclaration d'alias de type (C++11)
attributs (C++11)
Jette
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversions implicites
conversion const_cast
conversion static_cast
conversion dynamic_cast
conversion reinterpret_cast
conversions style C et style fonction
Allocation de mmoire
Classes
Qualificatifs spcifiques aux membres de classe
Fonctions membres spciales
Modles
classes gnriques
fonctions gnriques
spcialisation de modles
paquets de paramtres (C++11)
Divers
Assembleur
 
Un modle de classe dfinit une famille de classes .
Original:
A class template defines a family of classes.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Syntaxe

template < parameter-list > declaration

Explication

declaration dfinit ou dclare une classe (y compris struct et union), un membre de classe ou de type d'numration membre, un fonction ou fonction membre, une donne membre statique d'un modle de classe, ou un alias de type. Il peut galement dfinir une spcialisation de template. Cette page est consacre modles de classe .
Original:
declaration defines or declares a class (including struct and union), a member class or member enumeration type, a fonction ou fonction membre, a static data member of a class template, or a alias de type. It may also define a spcialisation de template. This page focuses on class templates.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
parameter-list est un non-vide spares par des virgules liste des paramtres de modle, dont chacun reprsente l'un des paramtres non-type, un type de paramtre, un paramtre de modle, ou une Paquet de paramtre selon l'une quelconque de celles-ci. Cette page porte sur les paramtres qui ne sont pas les packs de paramtres .
Original:
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 Paquet de paramtre of any of those. This page focuses on the parameters that are not parameter packs.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Paramtre de modle non type

type name (1)
type name = default (2)
type ... name (3) (depuis C++11)
1)
Un paramtre de modle non-type avec un nom facultatif
Original:
A non-type template parameter with an optional name
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
Un paramtre de modle non-type avec un nom d'option et une valeur par dfaut
Original:
A non-type template parameter with an optional name and a default value
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
3)
Un modle non-Paquet de paramtre type avec un nom facultatif
Original:
A non-type template Paquet de paramtre with an optional name
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
type est l'un des types suivants (ventuellement cv-qualifi, les qualificatifs sont ignores)
Original:
type is one of the following types (optionally cv-qualified, the qualifiers are ignored)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • type intgral
    Original:
    integral type
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • numration
    Original:
    enumeration
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • pointeur l'objet ou la fonction
    Original:
    pointer to object or to function
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • rfrence lvalue pour objet ou pour fonctionner
    Original:
    lvalue reference to object or to function
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • pointeur vers l'objet membre ou un membre de fonction
    Original:
    pointer to member object or to member function
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • std::nullptr_t (depuis C++11)
Les types de tableaux et la fonction peut tre crite dans une dclaration de modle, mais elles sont automatiquement remplac par le pointeur de donnes et un pointeur de fonction selon le cas .
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Lorsque le nom d'un paramtre de modle non-type est utilis dans une expression dans le corps du modle de classe, c'est un prvalue non modifiable sauf si son type est un type rfrence lvalue .
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Tapez paramtre de modle

typename name (1)
class name (2)
typename|class name = default (3)
typename|class ... name (4) (depuis C++11)
1)
Un paramtre de modle type avec un nom facultatif
Original:
A type template parameter with an optional name
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
Exactement la mme chose que 1)
Original:
Exactly the same as 1)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
3)
Un paramtre de modle type avec un nom facultatif et par dfaut
Original:
A type template parameter with an optional name and a default
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
5)
Un modle de type Paquet de paramtre avec un nom facultatif
Original:
A type template Paquet de paramtre with an optional name
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Paramtre template template

template < parameter-list > name (1)
template < parameter-list > name = default (2)
template < parameter-list > ... name (3) (depuis C++11)
1)
Un paramtre de modle modle avec un nom facultatif
Original:
A template template parameter with an optional name
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
Un paramtre template modle avec un nom et une option par dfaut
Original:
A template template parameter with an optional name and a default
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
3)
Un modle de Paquet de paramtre modle avec un nom facultatif
Original:
A template template Paquet de paramtre with an optional name
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Instanciation du modle de classe

Un modle de classe en elle-mme n'est pas un type ou un objet, ou toute autre entit. Aucun code est gnr partir d'un fichier source qui contient les dfinitions de modle seulement. Pour tout code d'apparatre, un modle doit tre instanci: les arguments de modle doit tre assure afin que le compilateur peut gnrer une classe relle (ou d'une fonction, d'un modle de fonction) .
Original:
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).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Instanciation explicite

template class name < argument-list > ; (1)
extern template class name < argument-list > ; (2) (depuis C++11)
1)
Dfinition instanciation explicite
Original:
Explicit instantiation definition
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
Dclaration instanciation explicite
Original:
Explicit instantiation declaration
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Une dfinition explicite d'instanciation forces instanciation de la classe, struct ou union ils se rfrent. Il peut apparatre n'importe o dans le programme aprs la dfinition du modle, et pour une donne argument-list, est seulement autoris apparatre qu'une seule fois dans le programme .
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Une dclaration instanciation explicite (un modle externe) empche les instanciations explicites: le code qui entranerait autrement une instanciation implicite doit utiliser la dfinition instanciation explicite fournie ailleurs dans le programme .
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Instanciation implicite

Lorsque le code se rfre un modle dans un contexte qui ncessite un type compltement dfinie, ou lorsque l'intgralit du type affecte le code, et ce type particulier n'a pas t explicitement instanci, l'instanciation implicite se produit. Par exemple, quand un objet de ce type est construit, mais pas quand un pointeur vers ce type est construit .
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Cela vaut pour les membres de la classe template: moins que le membre est utilise dans le programme, il n'est pas instanci, et ne ncessite pas une dfinition .
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

template<class T> struct Z { void f() {} void g(); // never defined }; // template definition template struct 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

Les paramtres des modles non-type

Les limitations suivantes s'appliquent lors de l'instanciation des modles de classe qui ont des paramtres de modles non-type:
Original:
The following limitations apply when instantiating class templates that have non-type template parameters:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • Pour les types entiers et le calcul, l'argument modle fourni lors de l'instanciation doit tre une expression constante .
    Original:
    For integral and arithmetic types, the template argument provided during instantiation must be a constant expression.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Pour les pointeurs vers les objets, les arguments de modle doivent dsigner l'adresse d'un objet avec la dure de stockage statique et un lien (interne ou externe), ou une expression constante qui renvoie la valeur de pointeur null appropri .
    Original:
    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.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Pour les pointeurs vers les fonctions, les arguments valides sont des pointeurs vers des fonctions avec liens (ou des expressions constantes qui permettent d'valuer la valeur null valeurs de pointeur) .
    Original:
    For pointers to functions, the valid arguments are pointers to functions with linkage (or constant expressions that evaluate to null pointer values).
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Pour les paramtres de rfrence lvalue, l'argument fourni l'instanciation ne peut pas tre une mesure temporaire, un anonyme lvalue, ou un nom lvalue sans lien .
    Original:
    For lvalue reference parameters, the argument provided at instantiation cannot be a temporary, an unnamed lvalue, or a named lvalue with no linkage.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Pour des pointeurs vers les membres, l'argument doit tre un pointeur vers le membre exprime en &Class::Member ou une expression constante qui prend la valeur null valeur de pointeur .
    Original:
    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.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
En particulier, cela implique que les chanes littrales, les adresses des lments du tableau, et les adresses des membres non statiques ne peuvent pas tre utiliss comme arguments de modle d'instancier des modles dont les paramtres des modles non correspondante de type sont des pointeurs vers les donnes .
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Exemple

[edit]
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
}


Les paramtres des modles non-type

#include <iostream>

// simple non-type template parameter
template<int N>
struct S {
    int a[N];
};

template<const char*>
struct S2 {};

// complicated non-type example
template <
    char c, // integral type
    int (&ra)[5], // lvalue reference to object (of array type)
    int (*pf)(int), // pointer to function
    int (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 time
    void foo(char base) {
        ra[4] = pf(c - base);
    }
};

//  S2<"fail"> s2; // Error: string literal cannot be used
char 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';
}

Rsultat :

42

Voir aussi


Web Proxy Viewer  |  New URL  |  Original Page