Pregunta simple, ¿por qué no funciona the following (lo que implica una copia de ci
)?¿Por qué `const int ci = 2; std :: forward <int> (ci); `trabajo y cómo solucionarlo/solucionarlo?
#include <utility>
int main(){
const int ci = 2;
std::forward<int>(ci);
}
El problema se manifestó al escribir algunas cosas plantilla, en la que tengo un tipo simple titular de la siguiente manera. Para evitar copias innecesarias, utilizo el reenvío perfecto donde sea posible, pero parece ser la raíz del problema.
template<class T>
struct holder{
T value;
holder(T&& val)
: value(std::forward<T>(val))
{}
};
template<class T>
holder<T> hold(T&& val){
// T will be deduced as int, because literal `5` is a prvalue
// which can be bound to `int&&`
return holder<T>(std::forward<T>(val));
}
template<class T>
void foo(holder<T> const& h)
{
std::tuple<T> t; // contrived, actual function takes more parameters
std::get<0>(t) = std::forward<T>(h.value); // h.value is `const T`
}
int main(){
foo(hold(5));
}
Si necesita más información, hágamelo saber.
Cualquier idea para eludir este problema es muy apreciada.
Debe hacer esto: 'template struct holder {holder (T val): value (std :: move (val)) {}}; plantilla titular :: type> asimiento (T val) {return titular (std :: movimiento (val)); } '(Sí, soy consciente de que se ve abrumadoramente. :)) –
GManNickG
@GMan: Pero no quiero eliminar la referencia. :( – Xeo
Entonces llaman como 'hold (std :: Ref (x));': P – GManNickG