草庐IT

c++ - 按值或右值引用传递

对于启用移动的类,这两者之间有区别吗?structFoo{typedefstd::vectorVectype;Vectypem_vec;//thisorvoidbar(Vectype&&vec){m_vec=std::move(vec);}//thatvoidbar(Vectypevec){m_vec=std::move(vec);}};intmain(){Vectypemyvec{"alpha","beta","gamma"};Foofool;fool.bar(std::move(myvec));}我的理解是,如果你使用左值myvec你还需要引入constFoo::bar()的Vec