perfect-forwarding

Perfect forwarding with multiple passes over input arguments

我的梦境 提交于 2019-12-06 05:43:39
问题 Consider the following function accept that takes a "universal reference" of type T and forwards that to a parse<T>() function object with an overload for lvalues and one for rvalues: template<class T> void accept(T&& arg) { parse<T>()(std::forward<T>(arg), 0); // copy or move, depending on rvaluedness of arg } template<class T> class parse { // parse will modify a local copy or move of its input parameter void operator()(T const& arg, int n) const { /* optimized for lvalues */ } void

No matching function std::forward with lambdas

前提是你 提交于 2019-12-05 10:42:00
Consider the following code, inspired from Barry's answer to this question: // Include #include <tuple> #include <utility> #include <iostream> #include <type_traits> // Generic overload rank template <std::size_t N> struct overload_rank : overload_rank<N - 1> { }; // Default overload rank template <> struct overload_rank<0> { }; // Prepend argument to function template <std::size_t N, class F> auto prepend_overload_rank(F&& f) { using rank = overload_rank<N>; return [f = std::forward<F>(f)](rank, auto&&... args) -> decltype(auto) { return std::forward<F>(f)(std::forward<decltype(args)>(args)..

Perfect Forwarding Variadic Template to Standard Thread

强颜欢笑 提交于 2019-12-05 09:54:19
I'm trying to make a form of std::thread that puts a wrapper around the code executed in the thread. Unfortunately I can't get it to compile due likely to my poor understanding of rvalues and the Function templated type I'm trying to pass. Here's my code: #include <vector> #include <thread> #include <utility> void Simple2(int a, int b) {} template <typename Function, typename... Args> void Wrapper(Function&& f, Args&&... a) { f(std::forward<Args>(a)...); } class Pool { public: template <typename Function, typename... Args> void Binder(Function&& f, Args&&... a) { std::thread t(Wrapper<Function

Questions on lambda overloads, type conversions and perfect forwarding

喜你入骨 提交于 2019-12-05 09:35:18
This is a question on lambda overload sets and perfect forwarding and somewhat of a followup to a comment . For more context of how this is used see another related question . I have some questions on the below code snippet. Q1: For lambda overloads, I was using overload(Fs...) -> overload<Fs...> from this post , but then in this answer I saw overload(Fs&&...) -> overload<std::decay_t<Fs>...> . In what situations is this difference relevant? Q2: Why would you want to define the identity function below with return decltype(x)(x) and not just return x ? Q3: Can we consider foo(convert(std:

Why forwarding reference does not deduce to rvalue reference in case of rvalue?

半腔热情 提交于 2019-12-05 08:08:35
I understand that, given an expression initializing a forwarding/universal reference,lvalues are deduced to be of type T& and rvalues of type T (and not T&& ). Thus,to allow only rvalues, one need to write template<class T, enable_if<not_<is_lvalue_reference<T> >,OtherConds... > = yes> void foo(T&& x) {} and not, template<class T, enable_if<is_rvalue_reference<T>,OtherConds... > = yes> void foo(T&& x) {} My question is , why for forwarding references, rvalues are deduced to be of type T and not T&& ? I guess, if they are deduced as T&& then also same referencing collapsing rule works as T&& &&

How to store universal references

怎甘沉沦 提交于 2019-12-05 04:51:34
I need to store universal references inside a class (I am sure the referenced values will outlive the class). Is there a canonical way of doing so? Here is a minimal example of what I have come up with. It seems to work, but I'm not sure if I got it right. template <typename F, typename X> struct binder { template <typename G, typename Y> binder(G&& g, Y&& y) : f(std::forward<G>(g)), x(std::forward<Y>(y)) {} void operator()() { f(std::forward<X>(x)); } F&& f; X&& x; }; template <typename F, typename X> binder<F&&, X&&> bind(F&& f, X&& x) { return binder<F&&, X&&>(std::forward<F>(f), std:

Why doesn't `const int ci = 2; std::forward<int>(ci);` work and how to fix / workaround it?

早过忘川 提交于 2019-12-05 02:41:55
Simple question, why doesn't the following work (implying a copy of ci )? #include <utility> int main(){ const int ci = 2; std::forward<int>(ci); } prog.cpp: In function 'int main()': prog.cpp:6:23: error: no matching function for call to 'forward(const int&)' The problem manifested itself while writing some template stuff, where I have a simple holder type as follows. To avoid unnecessary copies, I use perfect forwarding where possible, but that turns out to be the root of the problem it seems. template<class T> struct holder{ T value; holder(T&& val) : value(std::forward<T>(val)) {} };

Perfect forwarding of functions to build a function list class

我的梦境 提交于 2019-12-04 16:30:50
Consider the following code that build a class storing functions. // Function list class template <class... F> struct function_list { template <class... G> constexpr function_list(G&&... g) noexcept : _f{std::forward<G>(g)...} { } std::tuple</* F... OR F&&... */> _f; }; // Function list maker template <class... F, class R = /* Can we compute the return type here? */> constexpr R make_function_list(F&&... f) { return function_list< /* decltype(std::forward<F>(f))... * OR F... * OR F&&... */>(std::forward<F>(f)...); } I would like these functions to be perfectly forwarded (regardless of whether

Correct usage of `for_each_arg` - too much forwarding?

瘦欲@ 提交于 2019-12-04 13:16:10
I'm really happy to have discovered for_each_arg(...) , which makes dealing with argument packs much easier. template<class F, class...Ts> F for_each_arg(F f, Ts&&...a) { return (void)initializer_list<int>{(ref(f)((Ts&&)a),0)...}, f; } I'm, however, confused on its correct usage. There are many arguments that need to be perfectly forwarded, but am I performing any unnecessary forwarding? Reading the code becomes harder with excessive fowarding. struct UselessContainer { // Expects a perfectly-forwarded item to emplace template<typename T> void add(T&&) { } }; // Creates an `UselessContainer`

Perfect forwarding with class template argument deduction

孤街浪徒 提交于 2019-12-04 12:26:22
问题 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