Namespace
Varianti

std::forward

Da cppreference.com.
< cpp‎ | utility

 
 
Utilità libreria
Tipo di supporto (basic types, RTTI, type traits)
Gestione della memoria dinamica
La gestione degli errori
Programma di utilità
Funzioni variadic
Data e ora
Funzione oggetti
initializer_list(C++11)
bitset
hash(C++11)
Gli operatori relazionali
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>=
Coppie e tuple
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.
pair
tuple(C++11)
piecewise_construct_t(C++11)
piecewise_construct(C++11)
Swap, in avanti e spostare
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.
swap
forward(C++11)
move(C++11)
move_if_noexcept(C++11)
declval(C++11)
 
Elemento definito nell'header <utility>
template< class T >
T&& forward( typename std::remove_reference<T>::type& t );
(1) (dal C++11)
template< class T >
T&& forward( typename std::remove_reference<T>::type&& t );
(2) (dal C++11)
Quando viene utilizzato secondo la seguente ricetta in un modello di funzione, in avanti l'argomento di un'altra funzione esattamente come è stato passato alla funzione chiamante.
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 una chiamata a wrapper() passa un std::string rvalue, quindi T si deduce a std::string (non std::string&, const std::string& o std::string&&), e std::forward assicura che un riferimento rvalue viene passato a 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 una chiamata a wrapper() passa un const lvalue std::string, quindi T si deduce a const std::string&, e std::forward assicura che un const lvalue riferimento viene passato al 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 una chiamata a wrapper() passa un non-const std::string lvalue, quindi T si deduce a std::string&, e std::forward assicura che un riferimento non const lvalue viene passato al 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.

Indice

[modifica] Note

Il tentativo di trasmettere un rvalue come valore assegnabile, come ad esempio creando un'istanza del modulo 2) con riferimento lvalue tipo T, è un errore di compilazione.
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.

[modifica] Parametri

t -
l'oggetto da trasmettere
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.

[modifica] Valore di ritorno

static_cast<T&&>(t)

[modifica] Eccezioni

noexcept specification:  
noexcept
  (dal C++11)

[modifica] Esempio

Questo esempio dimostra trasmissione perfetta del parametro della funzione make_unique () per l'argomento del costruttore di 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
}

Output:

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

[modifica] Complessità

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

[modifica] Vedi anche

(C++11)
ottiene un riferimento 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.

(funzione di modello) [modifica]
ottiene un riferimento rvalue se il costruttore mossa non genera
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.

(funzione di modello) [modifica]