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. |
Contenido |
[editar] Descripción
Las plantillas permiten diseño función genérica que funcionen en varios tipos, sin la necesidad de volver a escribir varias veces
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.
[editar] Sintaxis
[editar] Declaración
template < template_arguments > function_declaration
|
(1) | ||||||||
export template < template_arguments > function_declaration
|
(2) | (hasta C++11) | |||||||
# Template declaración de la función Error en la cita: Etiqueta de apertura
<ref>
sin su correspondiente cierre </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.
# Exportados declaración de plantilla de función. El cuerpo de la función se puede definir en un archivo separado Error en la cita: Etiqueta de apertura
<ref>
sin su correspondiente cierre </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.
[editar] Argumentos
class identifier | (1) | ||||||||
typename identifier | (2) | ||||||||
integral_type identifier | (3) | ||||||||
class identifier = type_name
|
(4) | (desde C++11) | |||||||
typename identifier = type_name
|
(5) | (desde C++11) | |||||||
integral_type identifier = const_expr
|
(6) | (desde C++11) | |||||||
Dentro de la función identifier se puede utilizar como un tipo
3) 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.
Dentro de la función identifier se puede utilizar como una constante
4-6) 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.
Los argumentos predeterminados
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.
[editar] Especialización
This section is incomplete Reason: partial specialization |
template <> ret function_name < template_args > ( func_args ) body
|
|||||||||
Especialización cambia la aplicación de la función de plantilla para los parámetros de plantilla específicos
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.
[editar] Llamar
function_name < template_args > ( func_args )
|
(1) | ||||||||
function_name ( unambiguous_func_args )
|
(2) | ||||||||
Argumentos de plantilla # explícitos, si func_args no coinciden perfectamente con los tipos como en la declaración de plantilla (con la template_args dada) la colada habitual se producirá
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.
# Implícitos argumentos de plantilla, se deducen de los argumentos de la función. No ambigüedad puede estar presente
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 |
[editar] Ejemplo
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 }
[editar] Vea también
[editar] Notas
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.