◐ Shell
clean mode source ↗

Function template - cppreference.com

De cppreference.com

<metanoindex/>

Descrição

Os modelos permitem projeto função genérica de que o trabalho em vários tipos, sem a necessidade de reescrevê-lo várias vezes

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.

Sintaxe

Declaração

template < template_arguments > function_declaration (1)
export template < template_arguments > function_declaration (2) (até C++11)

# Declaração da função Template Erro de citação: Elemento de fecho </ref> em falta para o elemento <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.

# Exportados declaração função de modelo. O corpo da função pode ser definida em um arquivo separado Erro de citação: Elemento de fecho </ref> em falta para o elemento <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.

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)

1-2)

Dentro da função identifier pode ser usado como um tipo

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)

Dentro da função identifier pode ser usado como uma 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)

Argumentos padrão

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.

Especialização

template <> ret function_name < template_args > ( func_args ) body

Especialização muda a implementação da função de modelo para os parâmetros do modelo 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.

Chamar

function_name < template_args > ( func_args ) (1)
function_name ( unambiguous_func_args ) (2)

Argumentos de modelo # explícitas, se func_args não combinam perfeitamente com os tipos como na declaração modelo (com o template_args dado) o elenco habitual irá ocorrer

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.

# Implícitas argumentos de modelo, deduzidos os argumentos da função. Nenhuma ambiguidade pode 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.

Exemplo

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
}

Consulte também

Notas

Original:

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