Skip to main content
Filter by
Sorted by
Tagged with
5 votes
4 answers
306 views

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; //...
Victor Tsang's user avatar
4 votes
1 answer
101 views

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::...
Stégosaure's user avatar
2 votes
1 answer
166 views

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 ...
xmllmx's user avatar
  • 44.6k
6 votes
1 answer
469 views

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 ) { ...
PkDrew's user avatar
  • 2,301
4 votes
1 answer
112 views

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 <...
Jason R's user avatar
  • 11.9k
0 votes
0 answers
54 views

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> #...
Jack Xu's user avatar
  • 25
4 votes
2 answers
152 views

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 ...
Tirafesi's user avatar
  • 1,509
1 vote
1 answer
111 views

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&...
Guy Meirson's user avatar
2 votes
1 answer
107 views

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<...
Searcher's user avatar
  • 147
3 votes
1 answer
126 views

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 ...
glades's user avatar
  • 5,374
13 votes
3 answers
948 views

Is "arg" an universal/forwarding-reference or an rvalue-reference? template<auto&& arg> struct test {};
Martin Fehrs's user avatar
  • 1,175
2 votes
1 answer
358 views

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> ...
Oersted's user avatar
  • 3,834
1 vote
2 answers
265 views

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 &&...
Chameleon's user avatar
  • 2,239
0 votes
0 answers
41 views

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<...
chaosink's user avatar
  • 1,503
3 votes
3 answers
305 views

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& ...
mahush's user avatar
  • 141
0 votes
2 answers
144 views

What I have: template<typename T> class Holder { T& held; public: Holder(T& h) : held(h) {} friend std::ostream& operator<<(std::...
Loki Astari's user avatar
3 votes
1 answer
369 views

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,...
Sam Coutteau's user avatar
1 vote
2 answers
86 views

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 ...
petabyte's user avatar
  • 1,597
0 votes
2 answers
139 views

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 <...
Arc_Angle's user avatar
2 votes
1 answer
174 views

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 ...
RaisinCrab's user avatar
1 vote
1 answer
128 views

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 ...
Andrey Rubliov's user avatar
1 vote
3 answers
251 views

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 ...
Fabio's user avatar
  • 2,307
2 votes
2 answers
122 views

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 = "...
Qwe Qwe's user avatar
  • 557
0 votes
2 answers
100 views

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 ...
Dr. Gut's user avatar
  • 3,335
0 votes
0 answers
89 views

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; ...
Scyla's user avatar
  • 1
-1 votes
1 answer
70 views

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 ...
tearfur's user avatar
  • 119
0 votes
1 answer
116 views

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) { ...
user23339093's user avatar
5 votes
3 answers
495 views

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&...
jozxyqk's user avatar
  • 17.7k
-1 votes
1 answer
101 views

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 << "\...
Lain's user avatar
  • 13
0 votes
1 answer
87 views

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 ...
metamorphosis's user avatar
3 votes
1 answer
288 views

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(...
Enlico's user avatar
  • 30.3k
0 votes
0 answers
106 views

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{...
user21796467's user avatar
0 votes
1 answer
141 views

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. ...
uni's user avatar
  • 613
1 vote
3 answers
539 views

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&&...
علي المطوع's user avatar
0 votes
1 answer
117 views

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> ...
uniea's user avatar
  • 21
0 votes
0 answers
71 views

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 <&...
coin cheung's user avatar
  • 1,147
1 vote
1 answer
89 views

Hello everyone, template<typename X> int mycall(const X& a) { std::cout << ">>> " << "int mycall(const X& a)" << std::endl; return ...
Suat Mutlu's user avatar
4 votes
1 answer
467 views

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); ...
jh0427's user avatar
  • 71
2 votes
0 answers
73 views

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 ...
Mati's user avatar
  • 781
1 vote
1 answer
68 views

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 ...
Mati's user avatar
  • 781
16 votes
2 answers
1k views

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. #...
Weijun Zhou's user avatar
  • 5,569
1 vote
2 answers
195 views

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... ...
Yves's user avatar
  • 12.6k
1 vote
2 answers
463 views

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 ...
HarryP2023's user avatar
3 votes
3 answers
452 views

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 ...
Jan Schultke's user avatar
  • 43.7k
2 votes
1 answer
104 views

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(...
sandthorn's user avatar
  • 2,898
0 votes
3 answers
325 views

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 ...
Kappy's user avatar
  • 27
0 votes
1 answer
163 views

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 ....
Shahrooz's user avatar
  • 340
0 votes
2 answers
212 views

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 ...
Ben's user avatar
  • 9,903
7 votes
1 answer
405 views

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::...
user21796467's user avatar
-1 votes
1 answer
630 views

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 ...
Leon's user avatar
  • 2,165

1
2 3 4 5
12