perfect-forwarding

Can't add perfect forwarding to wrapper function

妖精的绣舞 提交于 2019-12-04 12:23:00
While answering this question I wrote this working code, wrapping function passed in template arguments: template<typename Fn, Fn fn, typename... Args> auto wrapper(Args... args)->decltype(fn(args...)){ return fn(args...); } #define WRAPPER(FUNC) wrapper<decltype(&FUNC), &FUNC> Example usage (I use this code for testing): int min(int a, int b){ return (a<b)?a:b; } #include<iostream> using std::cout; int main(){ cout<<WRAPPER(min)(10, 20)<<'\n'; } Two people told me to use perfect forwarding . When I asked how to do this, one of them redirected me here . I read question, carefully read best

std::move Vs std::forward

怎甘沉沦 提交于 2019-12-04 12:07:37
问题 This seems to be most relavant question already asked. Whats the difference between std::move and std::forward But each answer is different and applies and says slightly different things. So I am confused. I have the following situation. Copy item into container The Copy item is C++03 so I understand that quite well. Construct item into container The Construct item into container I believe uses perfect forwarding correctly to forward the arguments through two functions to the constructor of T

Why forwarding return value is needed

微笑、不失礼 提交于 2019-12-04 03:58:21
In the doc of std::forward , it gave the following example: template<class T> void wrapper(T&& arg) { foo(forward<decltype(forward<T>(arg).get())>(forward<T>(arg).get())); } Why is forwarding of return value needed here? What's the cases where it is different to the following code: template<class T> void wrapper(T&& arg) { foo(forward<T>(arg).get()); } Let's break down the possibilities. T::get could return an lvalue reference (which is an lvalue expression), an rvalue reference (which is an xvalue expression), or a prvalue. The forward expression will convert the lvalue expression into... an

What kind of problems for not forwarding universal reference?

半世苍凉 提交于 2019-12-03 17:31:20
问题 As far as I know, in C++11, universal reference should always be used with std::forward , but I am not sure of what kind of problem can occur if std::forward is not used. template <T> void f(T&& x); { // What if x is used without std::forward<T>(x) ? } Could you provide some illustrations of problems that could occur in this situation ? 回答1: There is no such rule to always use std::forward with universal references . On the contrary, it can be dangerous to use std::forward all over the place

C++11 std::function and perfect forwarding

限于喜欢 提交于 2019-12-03 10:48:26
Why definition of std::function<>::operator() in the C++ standard is: R operator()(ArgTypes...) const; and not R operator()(ArgTypes&&...) const; ? One would think that to correctly forward parameters, we need the && and then use std::forward<ArgTypes>... in the function body when forwarding the call? I partially reimplemented std::function to test this and I found out that if I use the &&, I get "cannot bind 'xxx' lvalue to 'xxx&&'" from g++ when I try later to pass parameters by value to operator(). I thought that I got enough grasp of the rvalue/forwarding concepts, but still I cannot grok

detecting protected constructors of (possibly abstract) base class

╄→尐↘猪︶ㄣ 提交于 2019-12-03 10:35:29
I am experimenting with the new features of C++11. In my setup I would really love to use inheriting constructors, but unfortunately no compiler implements those yet. Therefore I am trying to simulate the same behaviour. I can write something like this: template <class T> class Wrapper : public T { public: template <typename... As> Wrapper(As && ... as) : T { std::forward<As>(as)... } { } // ... nice additions to T ... }; This works... most of the time. Sometimes the code using the Wrapper class(es) must use SFINAE to detect how such a Wrapper<T> can be constructed. There is however the

Forwarding of return values. Is std::forward is needed?

落花浮王杯 提交于 2019-12-03 09:06:09
问题 I am writing library which wraps a lot of functions and methods from other library. To avoid coping of return values I am applying std::forward like so: template<class T> T&& wrapper(T&& t) { f(t); // t passed as lvalue return std::forward<T>(t); } f returns void and takes T&& (or overloaded on valueness). Wrapper always returns wrappers's param and on returned value should preserve valuness of argument. Do I actually need to use std::forward in return ? Does RVO makes it superfluous? Does

std::move Vs std::forward

跟風遠走 提交于 2019-12-03 08:30:29
This seems to be most relavant question already asked. Whats the difference between std::move and std::forward But each answer is different and applies and says slightly different things. So I am confused. I have the following situation. Copy item into container The Copy item is C++03 so I understand that quite well. Construct item into container The Construct item into container I believe uses perfect forwarding correctly to forward the arguments through two functions to the constructor of T in emplaceBackInternal() (Please say otherwise if I am wrong). Move item into container My problem

Perfect forwarding with class template argument deduction

北城以北 提交于 2019-12-03 07:57:12
I would like to understand how deductions guides work with universal references and std::forward , in particular to create perfectly forwarding wrappers. The code below provides a code to experiment with a functor wrapper in two cases: one with an implicit deduction guide and one with an explicit deduction guide. I have put a lot of && and std::forward in comments, because I do not know where they are needed to achieve perfect forwarding. I would like to know where to put them, and where they are not needed. // Case with not conversion constructor template <class F> struct functor1 { explicit

Constructor using std::forward

岁酱吖の 提交于 2019-12-03 05:55:09
问题 To my knowledge, the two common ways of efficiently implementing a constructor in C++11 are using two of them Foo(const Bar& bar) : bar_{bar} {}; Foo(Bar&& bar) : bar_{std::move(bar)} {}; or just one in the fashion of Foo(Bar bar) : bar_{std::move(bar)} {}; with the first option resulting in optimal performance (e.g. hopefully a single copy in case of an lvalue and a single move in case of an rvalue), but needing 2 N overloads for N variables, whereas the second option only needs one function