perfect-forwarding

Why use a perfectly forwarded value (a functor)?

♀尐吖头ヾ 提交于 2019-11-27 10:19:41
问题 C++11 (and C++14) introduces additional language constructs and improvements that target generic programming. These include features such as; R-value references Reference collapsing Perfect forwarding Move semantics, variadic templates and more I was browsing an earlier draft of the C++14 specification (now with updated text) and the code in an example in §20.5.1, Compile-time integer sequences , that I found interesting and peculiar. template<class F, class Tuple, std::size_t... I> decltype

Perfect forwarding container element

拈花ヽ惹草 提交于 2019-11-27 08:21:44
问题 Similar to this question, but instead of perfect forwarding member of an object, I would like to know how to perfect forwarding elements of an STL container, i.e. similar to struct X {}; void f(X&); void f(X&&); template <typename Vector> void g(Vector&& v, size_t i) { if (is_lvalue_reference<Vector>::value) { f(v[i]); } else { f(move(v[i])); } } 回答1: namespace detail { template<class T, class U> using forwarded_type = std::conditional_t<std::is_lvalue_reference<T>::value, std::remove

C++ Unified Assignment Operator move-semantics

ぐ巨炮叔叔 提交于 2019-11-27 04:19:20
问题 EDIT: solved see comments --don't know how to mark as solved with out an answer. After watching a Channel 9 video on Perfect Forwarding / Move semantics in c++0x i was some what led into believing this was a good way to write the new assignment operators. #include <string> #include <vector> #include <iostream> struct my_type { my_type(std::string name_) : name(name_) {} my_type(const my_type&)=default; my_type(my_type&& other) { this->swap(other); } my_type &operator=(my_type other) { swap

Capturing perfectly-forwarded variable in lambda

浪子不回头ぞ 提交于 2019-11-27 03:38:15
template<typename T> void doSomething(T&& mStuff) { auto lambda([&mStuff]{ doStuff(std::forward<T>(mStuff)); }); lambda(); } Is it correct to capture the perfectly-forwarded mStuff variable with the &mStuff syntax? Or is there a specific capture syntax for perfectly-forwarded variables? EDIT: What if the perfectly-forwarded variable is a parameter pack? Is it correct to capture the perfectly-forwarded mStuff variable with the &mStuff syntax? Yes, assuming that you don't use this lambda outside doSomething . Your code captures mStuff per reference and will correctly forward it inside the lambda

How to combine std::bind(), variadic templates, and perfect forwarding?

狂风中的少年 提交于 2019-11-27 02:27:43
问题 I want to invoke a method from another, through a third-party function; but both use variadic templates. For example: void third_party(int n, std::function<void(int)> f) { f(n); } struct foo { template <typename... Args> void invoke(int n, Args&&... args) { auto bound = std::bind(&foo::invoke_impl<Args...>, this, std::placeholders::_1, std::forward<Args>(args)...); third_party(n, bound); } template <typename... Args> void invoke_impl(int, Args&&...) { } }; foo f; f.invoke(1, 2); Problem is, I

Perfect forwarding a member of object

北城余情 提交于 2019-11-27 01:52:12
问题 Suppose I have two struct s: struct X {}; struct Y { X x; } I have functions: void f(X&); void f(X&&); How do I write a function g() that takes Y& or Y&& but perfect forwarding X& or X&& to f() , respectively: template <typename T> void g(T&& t) { if (is_lvalue_reference<T>::value) { f(t.x); } else { f(move(t.x)); } } The above code illustrate my intention but is not very scalable as the number of parameters grows. Is there a way make it work for perfect forwarding and make it scalable? 回答1:

How would one call std::forward on all arguments in a variadic function?

和自甴很熟 提交于 2019-11-27 00:01:06
问题 I was just writing a generic object factory and using the boost preprocessor meta-library to make a variadic template (using 2010 and it doesn't support them). My function uses rval references and std::forward to do perfect forwarding and it got me thinking...when C++0X comes out and I had a standard compiler I would do this with real variadic templates. How though, would I call std::forward on the arguments? template <typename ...Params> void f(Params... params) // how do I say these are

Can I typically/always use std::forward instead of std::move?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 23:55:51
I've been watching Scott Meyers' talk on Universal References from the C++ and Beyond 2012 conference, and everything makes sense so far. However, an audience member asks a question at around 50 minutes in that I was also wondering about. Meyers says that he does not care about the answer because it is non-idiomatic and would silly his mind, but I'm still interested. The code presented is as follows: // Typical function bodies with overloading: void doWork(const Widget& param) // copy { // ops and exprs using param } void doWork(Widget&& param) // move { // ops and exprs using std::move(param)

Purpose of perfect forwarding for Callable argument in invocation expression?

巧了我就是萌 提交于 2019-11-26 22:04:15
问题 In Scott Meyer's book Effective Modern C++ on page 167 (of the print version), he gives the following example: auto timeFuncInvocation = [](auto&& func, auto&&... params) { // start timer; std::forward<decltype(func)>(func)( std::forward<decltype(params)>(params)... ); // stop timer and record elapsed time; }; I completely understand the perfect forwarding of params , but it is unclear to me when perfect forwarding of func would ever be relevant. In other words, what are the advantages of the

Is there a difference between universal references and forwarding references?

孤者浪人 提交于 2019-11-26 16:30:50
问题 An argument to this function will bind to an rvalue reference: void f(int && i); However, an argument to this function will bind to either an rvalue or an lvalue reference: template <typename T> void f(T && t); I've often heard this referred to as a universal reference. I've also heard it been called a forwarding reference. Do they mean the same thing? Is it only a forwarding reference if the function body calls std::forward ? 回答1: Do they mean the same thing? Universal reference was a term