std::forward
Da 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. |
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.
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()
passastd::string
rvalue, entãoT
é deduzido parastd::string
(nãostd::string&
,const std::string&
oustd::string&&
), e assegura questd::forward
uma referência de rvalue é passado parafoo
.Original:If a call towrapper()
passes an rvaluestd::string
, thenT
is deduced tostd::string
(notstd::string&
,const std::string&
, orstd::string&&
), andstd::forward
ensures that an rvalue reference is passed tofoo
.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 lvaluestd::string
, entãoT
é deduzido paraconst std::string&
, e garante que umastd::forward
const lvalue referência é passado parafoo
.Original:If a call towrapper()
passes a const lvaluestd::string
, thenT
is deduced toconst std::string&
, andstd::forward
ensures that a const lvalue reference is passed tofoo
.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-conststd::string
lvalue, entãoT
é deduzido parastd::string&
estd::forward
garante que uma referência não-const lvalue é passado parafoo
.Original:If a call towrapper()
passes a non-const lvaluestd::string
, thenT
is deduced tostd::string&
, andstd::forward
ensures that a non-const lvalue reference is passed tofoo
.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.
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
[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.
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.
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) |
(C++11) |
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) |