vendredi 1 janvier 2016

c++ rvalue reference forwarding performance

I am doing some experiments to try to understand how forwarding works and I get to situation where I do not understand.

When I compile with clang 3.8 -O3

class Foo {
  Foo(const std::string& s) : str(s) {}
  std::string str;
};

and

class Foo {
  Foo(std::string&& s) : str(std::forward<std::string&>(s)) {}
  std::string str;
};

Constructing Foo with Foo foo("this is a test") in the first case is almost 2 times faster.

Why?

Aucun commentaire:

Enregistrer un commentaire