566 questions
5
votes
4
answers
306
views
How to implement perfect forwarding for a container?
Consider the code fragment below:
using List=std::vector<Data>;
List generate();
List a = generate();
List b = generate();
a += generate(); // append generate() output to a
a += b; //...
4
votes
1
answer
101
views
Clang-tidy bugprone-use-after-move with perfect forwarding
clang-tidy reports the following piece of code as bugprone-use-after-move
template <typename F, typename Tuple, size_t... I>
auto transform_tuple_impl(F&& f, Tuple&& tuple, std::...
2
votes
1
answer
166
views
Should we still always std::forward a universal reference argument even if unnecessary?
Consider the following code:
#include <string>
auto f1(auto&& v) {
return v;
}
auto f2(auto&& v) {
return std::forward<decltype(v)>(v);
}
int main() {
return ...
6
votes
1
answer
469
views
Is const T& effectively the same as T&& with std::forward for rvalue argument when be passed into another function?
Say I have a const T& version:
template <typename T>
void yetAnotherFunc( const T &input ) {
// do something...
}
template <typename T>
void myFunc( const T &input ) {
...
4
votes
1
answer
112
views
Can I forward an array type to a function invoked via pointer perfectly, without the argument decaying?
I have an issue that boils down to an example like this:
#include <cstdlib>
#include <utility>
// class that is constructible from a `float` array type
struct data
{
template <...
0
votes
0
answers
54
views
Perfect forwarding is_const_v is false but T is still const [duplicate]
I am trying to run the following code snippet to test my understanding of how types mutate/get interpreted in the process of being passed around.
#include <string>
#include <iostream>
#...
4
votes
2
answers
152
views
Wrap std::thread in lambda with perfect forwarding
I need to wrap std::thread to do some processing (inside the new thread) before the user function runs.
At the moment I'm achieving this via a helper function, but that's a bit annoying.
How can I ...
1
vote
1
answer
111
views
How to make a forwarding reference for arrays of items?
I am learning right now about generic programming in C++ and I am trying to figure out how to forward reference an array of items.
I have managed to do perfect forwarding for regular items:
template&...
2
votes
1
answer
107
views
Template argument deduction in perfect forwarding [duplicate]
I'm learning perfect forwarding, and run the example code(listed bellow)
template<typename T>
void show_type(T t){
std::cout << typeid(t).name() << std::endl;
}
template<...
3
votes
1
answer
126
views
Why isn't the compiler able to deduce type of std::endl for my operator<<(T&&)?
I'm using perfect forwarding to pipe a type through to a associated JsonRecord object. However, compiler is unable to deduce the type of std::endl when I want to pick it up as a token to forward to ...
13
votes
3
answers
948
views
Perfect forwarding and non-type template parameters
Is "arg" an universal/forwarding-reference or an rvalue-reference?
template<auto&& arg>
struct test {};
2
votes
1
answer
358
views
std::forward_like error with clang and deducing this
I'm experimenting with deducing this in order to simplify a code base. I'm getting an issue with a straightforward test:
#include <iostream>
#include <type_traits>
#include <utility>
...
1
vote
2
answers
265
views
Universal references: Why deducing this does not have std::forward?
I watched a video of Scott Meyers about universal references and he told that anytime when you use a universal reference, you must forward it like that:
template<typename T>
auto func(T &&...
0
votes
0
answers
41
views
Keep the same l/r-valueness as input type for return type [duplicate]
I tried to implement it as the following, with all of auto, decltype() and std::forward<>().
Is there any simpler implementation?
#include <vector>
using namespace std;
template<...
3
votes
3
answers
305
views
Generic Perfect Forwarding of Function’s Result
I encountered an issue when trying to perfect forward a function’s result in a generic way.
Here two functions that provide a result:
Foo provideFooAsTemporary()
{
return Foo{};
}
Foo& ...
0
votes
2
answers
144
views
Template type deduction include the const?
What I have:
template<typename T>
class Holder
{
T& held;
public:
Holder(T& h)
: held(h)
{}
friend std::ostream& operator<<(std::...
3
votes
1
answer
369
views
Structured binding, passing them to other functions and value categories
The main question I have is:
How do you correctly write a function which applies structured binding and passes the resulting variables to a function, whilst correctly preserving references, const-ness,...
1
vote
2
answers
86
views
Constructor precedence with templatized constructor [duplicate]
I'm looking to create a wrapper pointer SpecialPtr and it is following rule of 5, but also has a templatized constructor below. When using the wrapper pointer in a vector, the vector class wants to ...
0
votes
2
answers
139
views
Is there a standard function for performing a move-or-copy in c++?
I have a function which takes an rvalue reference:
void foo(int&& x);
I would like to create another function which supplies a universal interface with the original function:
template <...
2
votes
1
answer
174
views
How to Ensure Type Correctness When Implementing Partial Application
As someone who isn't too familiar with the ins and outs of template metaprogramming yet, I wanted to take a shot at implementing a (naive) basic partial-application helper that binds arguments to the ...
1
vote
1
answer
128
views
C++ passing a string to class setter function [duplicate]
I see this subject was discussed in StackOverflow, but I could not find the right answer.
I've been using C++ for many years, and I am still in doubt about how to write a simple setter method for a ...
1
vote
3
answers
251
views
how to do perfect forwarding when the template argument is explicitly declared
Consider this simple function
template <typename U>
auto mkVector(U&& x0)
{
return std::vector<std::decay_t<U>>{std::forward<U>(x0)};
}
and 4 possible use cases ...
2
votes
2
answers
122
views
Parameter pack and perfect forwarding
I just wrote following simple code but it doesnt compile:
#include <iostream>
#include <string>
class Obj{
public:
std::string name = "Name";
std::string l_name = "...
0
votes
2
answers
100
views
Are there uses of std::forward, where the argument is not a variable name?
In most cases the type of std::forward's argument is a forwarding reference:
template <class T>
void CallF(T&& value) {
F(std::forward<T>(value));
}
In rarer cases the type is ...
0
votes
0
answers
89
views
How to prefectly forward member variable of an r-value objects in variadic context
While trying to optimize a library I wrote, I came across an issue that I cannot seem to solve.
Consider the wrapper struct below (simplified):
template <typename T>
struct MyPair {
T data;
...
-1
votes
1
answer
70
views
C++ How to allow copy-list-initialization for a perfect forwarding function?
Suppose I have a perfect forwarding function:
template <typename T>
void f(T&& t) { /* does something */ }
I have some types T that I want to allow Copy-list-initialization for, say ...
0
votes
1
answer
116
views
Is this a perfect forwarding function?
Is this a perfect forwarding function? Or does the creation of a temporary T make it non-perfect? T is a trivial class.
template<class T, typename... Args>
void foo(Args&&... args)
{
...
5
votes
3
answers
495
views
Is there a move-or-copy equivalent of std::forward for perfect forwarding in std::ranges?
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>
template&...
-1
votes
1
answer
101
views
Why can't I use universal reference in a way like `void(*func)(T&&)` and `for (T&& v : vec)`?
I'm trying to use a universal reference in a callback function pointer and range-based for loop.
template<typename T>
void PrintFunc(const T& value) {
cout << value << "\...
0
votes
1
answer
87
views
C++ - Interaction between move construction and forwarding
If I'm constructing a non-trivial container like a custom map for example,
and want to share code between the emplace and insert functions, one approach would be to have the insert function simply ...
3
votes
1
answer
288
views
When would I want to use auto&& instead of decltype(auto) or ->decltype(return-expr) for the return type of a function definition?
Take three functions returning prvalue, lvalue, xvalue:
int f();
int& f(int);
int&& f(int, int);
and call them via a function returning decltype(auto)
decltype(auto) returnsDecltypeOf(...
0
votes
0
answers
106
views
Difference between instantiating a tuple with explicitly specified types using std::make_tuple and using the tuple constructor [duplicate]
Take for example the code snippet bellow that instantiates some tuple instances using the std::make_tuple function and the tuple constructor:
...
const auto integer = 0;
const auto text = std::string{...
0
votes
1
answer
141
views
Universal reference in variadic template class is treated as rvalue reference [duplicate]
I am trying to create variadic template that can get lvalues, rvalues and rvalue references as arguments. But it seems that T&& is treated as rvalue reference and wouldn't accept other types.
...
1
vote
3
answers
539
views
using std::forward on the same argument in a loop
I know there are cases where using std::forward on the same argument in a loop is wrong because it may cause moving from a moved object, like so:
template <typename T>
auto applyTenTimes(T&&...
0
votes
1
answer
117
views
Why std::forward return a rvalue when transferring a lvalue? [duplicate]
It is said that std::forward() perfectly forwards a lvalue to lvalue, and rvalue to rvalue. But the code below seems to show a different conclusion?
when I run the code below
#include <iostream>
...
0
votes
0
answers
71
views
Why is c++ std::forward always return right value without template
I am trying to understand c++ perfect forwarding, and I do experiments with the following code:
#include <utility>
#include <iostream>
using namespace std;
void inner(int&) {cout <&...
1
vote
1
answer
89
views
perfect forwarding confusion
Hello everyone,
template<typename X>
int mycall(const X& a)
{
std::cout << ">>> " << "int mycall(const X& a)" << std::endl;
return ...
4
votes
1
answer
467
views
C++ concept and perfect forwarding
I have a relatively simple concept definition of a container type which takes in a specific value type:
template <typename T>
concept FooContainer = requires(T cont){
std::begin(cont);
...
2
votes
0
answers
73
views
Why copy of a member is returned rather than reference?
According to cppreference member of an object which is lvalue is also an lvalue:
The following expressions are lvalue expressions:
a.m, the member of object expression, except where m is a member ...
1
vote
1
answer
68
views
Is forward needed to perfectly pass output of a call to another call?
According to the (cppreference) there is a need to use std::forward() in order to perfectly pass outcome of a function passed to another call without storing it explicitly elsewhere.
[...] For ...
16
votes
2
answers
1k
views
How to perfect forward variadic template args with default argument std::source_location?
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
vote
2
answers
195
views
How to pass template parameter of a function to a lambda in the function
template <typename C, typename F, typename... Args>
void MyClass::run(F C::*f, C* c, Args&&... args) {
td_ = std::make_unique<std::thread>(
[](F C::*f, C* c, Args... ...
1
vote
2
answers
463
views
Range for loop for empty initializer list
I was reading about forwarding references on cpp reference https://en.cppreference.com/w/cpp/language/reference#Forwarding_references and I was interested to learn that there is a special case for ...
3
votes
3
answers
452
views
Can you perfectly forward expressions in C++23?
In C++23, it became easier to perfectly forward prvalues thanks to auto() or auto{}. With this new tool, is it now possible to form a FORWARD(e) expression for an expression e with the following ...
2
votes
1
answer
104
views
According to the "auto(x)" paper (wg21.link/p0849) , how come "return std::forward<T>" fails to perfect forward the parameters typed with "T&&"?
The auto(x) expression is added to the language. A rational is because we can not perfect forward decay with this.
template<class T>
constexpr decay_t<T> decay_copy(T&& v) noexcept(...
0
votes
3
answers
325
views
std::move and static_cast<&&>
Do I understand right, if I call std::move() with a temporary object, like std::move(A()) or std::move(int), it returns a reference to free memory?
As I know, if T&& or const T& assigns to ...
0
votes
1
answer
163
views
Problem with storing forward references(universal) to be forward and used later
Problem with storing forward references(universal) to be forward and used later
I'm writing a class where I can pass any function pointer with its argument types and return type, and later call the ....
0
votes
2
answers
212
views
Forwarding the value category of a container?
In C++20 I want to write generic code to take a container or a view and perfectly forward each element. That is, if I have an obnoxious type like this:
struct S {
struct tag {}; // So we have a ...
7
votes
1
answer
405
views
How to perfect forward the elements of a function parameter pack in C++
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::...
-1
votes
1
answer
630
views
How to perfectly forward a universal reference that is either a const ref or a movable rvalue?
I have coded a lock-free and thread-safe ring queue with C++20, and it works so far. The only thing is not perfect that it has to have two enque() methods, one accepts a const reference to a lvalue as ...