2

Both std::move and std::forward are template functions, why is it that we don't need to provide a template argument type for std::move but std::forward?

For example (from Effective Modern C++):

class Widget {
public:
    Widget(Widget&& rhs)
    : s(std::move(rhs.s)) {} 
...

private:
    std::string s;
};
class Widget {
public:
    Widget(Widget&& rhs)
    : s(std::forward<std::string>(rhs.s)) {}
...
};
Lol4t0
  • 12,444
  • 4
  • 29
  • 65
sh8888
  • 46
  • 2
  • 2
    Hm... @KerrekSB Is your dupe really answering the question? I guess a good answer would explain why template type deduction fails (/ should fail) for `std::forward` but doesn't for `std::move`, which I can't find in the dupe. – leemes Jul 24 '14 at 14:30
  • 6
    The question you should be asking is why doesn't `std::forward` use template argument deduction, and that is explained [here](http://stackoverflow.com/questions/7779900/why-is-template-argument-deduction-disabled-with-stdforward). – Praetorian Jul 24 '14 at 14:30
  • @Praetorian Yeah I agree that that's a good dupe, the only missing piece would be why `std::move` is *different* in that point, which is ok if one knows that template type deduction exists in the first place. – leemes Jul 24 '14 at 14:33
  • @leemes: Hm, maybe, but that should still mean that the other question should get improved or new answers, so we don't spread this out so much. Well, I don't mind, if someone wants to reopen this, go ahead. – Kerrek SB Jul 24 '14 at 14:42

0 Answers0