samedi 27 juin 2015

C++ what is best practices to use value / lvalue / rvalue

i have test function like this:

list_add(IList &list, Pair &p){
    list.put(p);
}

// this works
Pair p1{"city", "Sofia" };
size += list_add(list, p1);

// this does not compile.
size += list_add(list, Pair{"name", "Niki"  });

I know second not working, because lvalue requires variable or needs to be const. At the moment I can not use const.

My question is - shall I change it to pass by value

list_add(IList &list, Pair p);

or shall I use rvalue, must looks like this, no idea if it will compile

list_add(IList &list, Pair &&p);

Aucun commentaire:

Enregistrer un commentaire