código va a continuación:vector <T> :: swap y objeto temporal
#include <vector>
int main()
{
vector<int> v1(5,1);
v1.swap(vector<int>()); //try to swap v1 with a temporary vector object
}
El código anterior no se puede compilar, error:
error: no matching function for call to ‘std::vector<int, std::allocator<int> >::swap(std::vector<int, std::allocator<int> >)’
Pero, si cambio el código para algo como esto, puede compilar:
int main()
{
vector<int> v1(5,1);
vector<int>().swap(v1);
}
¿Por qué?
¿Podría por favor ofrecer más información sobre "' vector() 'is an rvalue"? 'rvalue' debería aparecer en el lado derecho de' = ', ¿verdad? –
Alcott
@Alcott: es más general que eso. Ver http://en.wikipedia.org/wiki/Value_(computer_science). –
Encontré este artículo muy útil para entender los valores r: http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++.html – Adam