perfect-forwarding

What's the intention of forward-by-lvalue-reference constructor while a perfect forwarding constructor exists?

可紊 提交于 2019-12-08 17:38:14
问题 Let's take std::pair<T1, T2> as an example. It has the following two constructors: constexpr pair( const T1& x, const T2& y ); // #1 template< class U1, class U2 > constexpr pair( U1&& x, U2&& y ); // #2 It seems that #2 can handle all cases that #1 can handle (without worse performance), except for cases where an argument is a list-initializer. For example, std::pair<int, int> p({0}, {0}); // ill-formed without #1 So my question is: If #1 is only intended for list-initializer argument, since

When not to use std::forward with r-values?

北城以北 提交于 2019-12-08 17:20:39
问题 What are the cases when std::forward is not needed? It is used to wrap inner function argument which is templated-rvalue (that is, it can be lvalue or named-rvalue). Like in: template<class T> void outer(T&& t) { inner(std::forward<T>(t)); } I am guessing one case is when inner function parameters are passed by value. Are there other cases? I've got this question when I was writing std::begin(std::forward<Ct>(ct)) where Ct is templated-rvalue-ref. EDIT about possible duplicate If I remember

Is there a way to forward argument to inner constexpr function?

痴心易碎 提交于 2019-12-08 16:35:25
问题 The question: is it possible to evaluate constant expression inside a function by passing (maybe with some kind of "perfect forwarding") its argument to inner constexpr function? Example: constexpr size_t foo(char const* string_literal) { return /*some valid recursive black magic*/; } void bar(char const* string_literal) { // works fine constexpr auto a = foo("Definitely string literal."); // compile error: "string_literal" is not a constant expression constexpr auto b = foo(string_literal);

Converting a variadic macro to a variadic template function?

喜夏-厌秋 提交于 2019-12-08 06:54:53
问题 Given a variadic macro of the form: #define MY_CALL_RETURN_F(FType, FId, ...) \ if(/*prelude omitted*/) { \ FType f = (FType)GetFuncFomId(FId); \ if(f) { \ return f(__VA_ARGS__); \ } else { \ throw invalid_function_id(FId); \ } \ } \ /**/ -- how can this be rewritten to a variadic function template? template<typename FType, typename ...Args> /*return type?*/ tmpl_call_return_f(MyFunId const& FId, /*what goes here?*/) { ... FType f = (FType)GetFuncFomId(FId); return f(/*what goes here?*/); ...

Questions on lambda overloads, type conversions and perfect forwarding

♀尐吖头ヾ 提交于 2019-12-07 06:39:35
问题 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

Perfect Forwarding Variadic Template to Standard Thread

て烟熏妆下的殇ゞ 提交于 2019-12-07 05:29:58
问题 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

Capturing generic callable objects in nested lambdas - always forward?

可紊 提交于 2019-12-06 14:04:45
I have various functions in my codebase that take a generic callable object and pass it to a series of nested lambdas before calling it. Example: template <typename TF> void interface(TF&& f) { nested0([/*...*/]() { nested1([/*...*/](auto& x) { nested2([&x, /*...*/]() { f(x); }); }); }); } Note that interface is taking a callable object of type TF by forwarding reference (previously known as universal reference) . The callable object is usually a lambda with various captured variables, both by value and by reference. What is the best (in terms of performance) way of capturing f in the nested

Can't add perfect forwarding to wrapper function

怎甘沉沦 提交于 2019-12-06 08:48:34
问题 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 .

Perfect forwarding of functions to build a function list class

孤者浪人 提交于 2019-12-06 07:25:43
问题 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&&... */>

Correct usage of `for_each_arg` - too much forwarding?

隐身守侯 提交于 2019-12-06 06:36:51
问题 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