Function template
De cppreference.com
[] |
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
<metanoindex/>
Description
Des modles de conception permettent fonction gnrique qui fonctionnent sur diffrents types, sans la ncessit de rcrire 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.
You can help to correct and verify the translation. Click here for instructions.
Syntaxe
Dclaration
template < template_arguments > function_declaration
|
(1) | ||||||||
export template < template_arguments > function_declaration
|
(2) | (avant C++11) | |||||||
# Modle de dclaration de fonction Erreur de rfrence: 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.
You can help to correct and verify the translation. Click here for instructions.
# Exports dclaration de fonction template. Le corps de la fonction peut tre dfinie dans un fichier spar Erreur de rfrence: 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.
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'intrieur 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.
You can help to correct and verify the translation. Click here for instructions.
3)
l'intrieur 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.
You can help to correct and verify the translation. Click here for instructions.
4-6)
Les arguments par dfaut
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.
You can help to correct and verify the translation. Click here for instructions.
Spcialisation
| This section is incomplete Reason: partial specialization |
template <> ret function_name < template_args > ( func_args ) body
|
|||||||||
Spcialisation modifie la mise en uvre de la fonction de modle pour les paramtres des modles spcifiques
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.
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 dclaration de modle (avec le template_args donn) la coule 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.
You can help to correct and verify the translation. Click here for instructions.
Arguments template # implicites, dduites des arguments de la fonction. Aucune ambigut ne peut tre prsent
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.
You can help to correct and verify the translation. Click here for instructions.
| This section is incomplete Reason: better explanation/example |
Exemple
| This section is incomplete |
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.
You can help to correct and verify the translation. Click here for instructions.