Espaces de noms
Variantes

Function template

De cppreference.com

<metanoindex/>

 
 
Langage C++
Sujets généraux
Contrôle de flux
Instructions conditionnelles
Instructions d'itération
Instructions de saut
Fonctions
déclaration de fonction
expression lambda
fonction générique
spécificateur inline
spécification d'exception (obsolète)
spécificateur noexcept (C++11)
Exceptions
Espaces de noms
Types
spécificateur decltype (C++11)
Qualificatifs
qualificatifs const et volatile
qualificatifs de stockage
qualificatif constexpr (C++11)
qualificatif auto (C++11)
qualificatif alignas (C++11)
Initialisation
Littéraux
Expressions
opérateurs alternatifs
Utilitaires
Types
déclaration typedef
déclaration 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 mémoire
Classes
Qualificatifs spécifiques aux membres de classe
Fonctions membres spéciales
Modèles
classes génériques
fonctions génériques
spécialisation de modèles
paquets de paramètres (C++11)
Divers
Assembleur
 

Description

Des modèles de conception permettent fonction générique qui fonctionnent sur différents types, sans la nécessité de réécrire plusieurs fois
Original:
Templates allow generic function design that work on various types, without the need of rewriting it multiple times
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Syntaxe

Déclaration

template < template_arguments > function_declaration (1)
export template < template_arguments > function_declaration (2) (avant C++11)
# Modèle de déclaration de fonction Erreur de référence : Balise fermante </ref> manquante pour la balise <ref>
Original:
{{{2}}}
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
# Exportés déclaration de fonction template. Le corps de la fonction peut être définie dans un fichier séparé Erreur de référence : Balise fermante </ref> manquante pour la balise <ref>
Original:
{{{2}}}
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Arguments

class identifier (1)
typename identifier (2)
integral_type identifier (3)
class identifier = type_name (4) (depuis C++11)
typename identifier = type_name (5) (depuis C++11)
integral_type identifier = const_expr (6) (depuis C++11)

1-2)

À l'intérieur de la fonction identifier peut être utilisé comme un type
Original:
Inside the function identifier can be used as a type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

3)

À l'intérieur de la fonction identifier peut être utilisé comme une constante
Original:
Inside the function identifier can be used as a constant
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

4-6)

Les arguments par défaut
Original:
Default arguments
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Spécialisation

template <> ret function_name < template_args > ( func_args ) body
Spécialisation modifie la mise en œuvre de la fonction de modèle pour les paramètres des modèles spécifiques
Original:
Specialization changes the implementation of the template function for specific template parameters
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Appel

function_name < template_args > ( func_args ) (1)
function_name ( unambiguous_func_args ) (2)
Arguments template # explicites, si func_args ne correspondent pas parfaitement avec les types comme dans la déclaration de modèle (avec le template_args donné) la coulée d'habitude va se produire
Original:
# Explicit template arguments, if func_args don't match perfectly with the types as in the template declaration (with the given template_args) the usual casting will occur
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Arguments template # implicites, déduites des arguments de la fonction. Aucune ambiguïté ne peut être présent
Original:
# Implicit template arguments, deduced from the function arguments. No ambiguity can be present
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
}


Voir aussi

Notes

Original:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.