vendredi 25 septembre 2015

universal reference c++11 code duplication

In my project I have function like this:

bool VectorList::put(const Pair &p);

This adds the Pair into the VectorList by copying the Pair.

I can use it like this:

Pair p { "key", "value" };

VectorList v;
v.put(p);

// or
v.put(Pair{ "anotherkey", "anothervalue" });

However in second case an unnecessary object is created, so I want to do

bool VectorList::put(Pair &&p);

I checked how this is done in vector (gcc, llvm) and there is 100% same code in both methods, except equal / std::move() line.

Is there some way I could do it without code duplication?

Aucun commentaire:

Enregistrer un commentaire