Home Why does auto return type lose move semantics in this example?
Reply: 0

Why does auto return type lose move semantics in this example?

user6331
1#
user6331 Published in September 21, 2018, 8:52 am

I was watching the video where Nicolai says that auto loses move semantics for this example:

template<typename Callable, typename... Args>
auto call(Callable&& op, Args&&... args) {
return std::invoke(std::forward<Callable>(op), std::forward<Args>(args)...);
}

I was wondering:

  1. why is this the case?

  2. does guaranteed RVO kick in in this example? If so what is the point of worrying about move?

share|improve this question
  • 3
    I think the issue with this code is that if callable returns a reference call will return an object constructed from that reference. So use of auto to declare a return type will make it forward "not so perfectly". And the video suggests to use decltype(auto) instead. – VTT Feb 12 at 12:51

1 Answer 1

active oldest votes
up vote 8 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.360772 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO