Espaços nominais
Variantes
Acções

std::forward

Da cppreference.com
< cpp‎ | utility

 
 
Biblioteca de utilitários
Digite apoio (basic types, RTTI, type traits)
Gerenciamento de memória dinâmica
De tratamento de erros
Utilidades do programa
Variadic funções
Data e hora
Objetos de função
(C++11)
Os operadores relacionais
Original:
Relational operators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rel_ops::operator!=
rel_ops::operator>
rel_ops::operator<=
rel_ops::operator>=
Pares e tuplas
Original:
Pairs and tuples
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(C++11)
Troque, avançar e avançar
Original:
Swap, forward and move
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
forward
(C++11)
(C++11)
(C++11)
 
Definido no cabeçalho <utility>
template< class T >
T&& forward( typename std::remove_reference<T>::type& t );
(1) (desde C++11)
template< class T >
T&& forward( typename std::remove_reference<T>::type&& t );
(2) (desde C++11)
Quando utilizado de acordo com a seguinte receita em um modelo de função, encaminha o argumento para outra função exatamente como ele foi passado para a função de chamada.
Original:
When used according to the following recipe in a function template, forwards the argument to another function exactly as it was passed to the calling function.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
template<typename T>
wrapper(T&& arg) {
  foo(std::forward<T>(arg));
}


  • Se uma chamada para um wrapper() passa std::string rvalue, então T é deduzido para std::string (não std::string&, const std::string& ou std::string&&), e assegura que std::forward uma referência de rvalue é passado para foo.
    Original:
    If a call to wrapper() passes an rvalue std::string, then T is deduced to std::string (not std::string&, const std::string&, or std::string&&), and std::forward ensures that an rvalue reference is passed to foo.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Se uma chamada para um const wrapper() passa lvalue std::string, então T é deduzido para const std::string&, e garante que uma std::forward const lvalue referência é passado para foo.
    Original:
    If a call to wrapper() passes a const lvalue std::string, then T is deduced to const std::string&, and std::forward ensures that a const lvalue reference is passed to foo.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Se uma chamada para wrapper() passa um não-const std::string lvalue, então T é deduzido para std::string& e std::forward garante que uma referência não-const lvalue é passado para foo.
    Original:
    If a call to wrapper() passes a non-const lvalue std::string, then T is deduced to std::string&, and std::forward ensures that a non-const lvalue reference is passed to foo.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

Índice

[editar] Notas

A tentativa de transmitir um rvalue como um lvalue, como por instanciar o formulário 2) com referência lvalue tipo T, é um erro de tempo de compilação.
Original:
Attempting to forward an rvalue as an lvalue, such as by instantiating the form 2) with lvalue reference type T, is a compile-time error.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[editar] Parâmetros

t -
o objeto a ser encaminhado
Original:
the object to be forwarded
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[editar] Valor de retorno

static_cast<T&&>(t)

[editar] Exceções

noexcept specification:  
noexcept
  (desde C++11)

[editar] Exemplo

Este exemplo demonstra o encaminhamento perfeito do parâmetro da make_unique function () para o argumento do construtor da classe T
Original:
This example demonstrates perfect forwarding of the parameter of the function make_unique() to the argument of the constructor of class T
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <iostream>
#include <memory>
#include <utility>
 
struct A {
    A(int&& n) { std::cout << "rvalue overload, n=" << n << "\n"; }
    A(int& n)  { std::cout << "lvalue overload, n=" << n << "\n"; }
};
 
template<class T, class U>
std::unique_ptr<T> make_unique(U&& u)
{
    return std::unique_ptr<T>(new T(std::forward<U>(u)));
}
 
int main()
{
    std::unique_ptr<A> p1 = make_unique<A>(2); // rvalue
    int i = 1;
    std::unique_ptr<A> p2 = make_unique<A>(i); // lvalue
}

Saída:

rvalue overload, n=2
lvalue overload, n=1

[editar] Complexidade

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

[editar] Veja também

(C++11)
obtém uma referência rvalue
Original:
obtains an rvalue reference
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(modelo de função) [edit]
obtém uma referência rvalue se o construtor movimento não joga
Original:
obtains an rvalue reference if the move constructor does not throw
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(modelo de função) [edit]