perfect-forwarding Questions

4

Solved

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...
Portwin asked 18/10, 2012 at 6:4

5

Solved

I am attempting to employ C++17 fold expressions and the C++14 indices trick to flatten an arbitrary input consisting of tuples and non-tuples. The expected result should at least conform to these...
Alaynaalayne asked 28/2, 2019 at 17:41

3

Solved

How can I combine the following two functions into one? Is there something similar to std::forward, but for ranges? #include <ranges> #include <vector> #include <algorithm> templ...
Gregggreggory asked 22/1, 2024 at 5:49

8

In perfect forwarding, std::forward is used to convert the named rvalue references t1 and t2 to unnamed rvalue references. What is the purpose of doing that? How would that affect the called functi...
Underglaze asked 27/8, 2010 at 6:59

2

Solved

I want to track where the creation of an object occurs using std::source_location. The following is a simplified example without the extra code to account for copy and movement of a Tracked object....

1

Solved

I am having trouble understanding how to forward the elements of a parameter pack in C++. Please take for example the code below: #include <iostream> void print(const int& value) { std:...

3

What's the cleanest way to perfectly forward arguments into a lambda capture in C++20/C++23? By this I mean capturing rvalues by copy and lvalues by reference, inside of the coroutine object: struc...
Labelle asked 21/7, 2022 at 12:30

2

Solved

I have the following code which is used to call a function on an object and pass any argument in a perfect-forwarding way template <typename F, typename T> inline auto call_with_args(F&&a...

2

Solved

The following code fails to compile: #include <iostream> using namespace std; int add2(const int& x) { return x + 2; } template <typename T> T add2T(T&& x) { return ad...
Ansilma asked 1/12, 2017 at 17:14

1

Solved

Given this class hierarchy: #include <iostream> class Base { public: Base() = default; Base(const Base&) { std::cout << " copy\n"; } template<typename T> Base...
Tret asked 28/8, 2021 at 8:55

4

I suppose when a universal reference parameter is matched with an rvalue reference argument, an rvalue reference argument is returned. However, my testing shows that the rvalue reference is t...

4

Solved

I saw this here: Move Constructor calling base-class Move Constructor Could someone explain: the difference between std::move and std::forward, preferably with some code examples? How to think a...
Firecure asked 12/3, 2012 at 17:20

2

Solved

I've understood how std::move works and implemented my own version for practice only. Now I'm trying to understand how std::forward works: I've implemented this so far: #include <iostream> ...
Retroflex asked 1/12, 2020 at 20:43

2

Solved

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: templa...
Indulgence asked 25/11, 2013 at 14:44

4

Solved

Consider this quote from C++ Templates: The Complete Guide (2nd Edition): decltype(auto) ret{std::invoke(std::forward<Callable>(op), std::forward<Args>(args)...)}; ... return ret; ...
Adagietto asked 28/2, 2018 at 16:10

1

Solved

In C++, it seems that a parameter may be normally expanded with ... directly after the parameter pack's name. For example, template <class... Tys> void function(Tys... params) { funct...

3

Solved

I have a class that is simply forwarding the function call to another class and I would like to be able to use std::invocable<> on my forwarding class. But for some reason that fails... Is th...
Pizzeria asked 20/3, 2020 at 13:58

2

Solved

I've been switching Template Factory functions to use (and understand) std::forward to support rvalues and move semantics. My usual boilerplate factory functions for template classes have always ma...

1

In below code I could not understand why move constructor of class is called twice considering that my thread function is taking argument by rvalue reference and so I was hoping move construc...
Intertwist asked 16/5, 2018 at 5:11

1

Solved

I noticed that aggregate list initalization of std::vector performs copy initialization when move is more applicable. At the same time, multiple emplace_backs do what I want. I could only come up ...
Beanery asked 8/1, 2020 at 23:52

5

Solved

template<typename T> void doSomething(T&& mStuff) { auto lambda([&mStuff]{ doStuff(std::forward<T>(mStuff)); }); lambda(); } Is it correct to capture the perfectly-forwa...
Flipper asked 9/11, 2014 at 18:0

3

Solved

I've got a C++ member function of a class with a const and non-const overloading. Class Example { public: int const & Access() const; int & Access(); [...] }; I wish for the const ve...
Conductivity asked 19/8, 2019 at 22:57

1

I'm reading Scott's book effective modern c++. In item 26, there's an example that I wrote on Wandbox: https://wandbox.org/permlink/6DKoDqg4jAjA9ZTB I want to verify how much the good code is bett...
Unabridged asked 26/8, 2019 at 11:30

4

Solved

Consider this minimal example template <class T> class Foo { public: Foo(const T& t_) : t(t_) { } Foo(T&& t_) : t(std::move(t_)) { } T t; }; template <typename F&g...
Caesura asked 1/8, 2019 at 15:26

2

Solved

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).g...
Apocopate asked 2/6, 2019 at 16:23

© 2022 - 2025 — McMap. All rights reserved.