Questions tagged [reference-wrapper]

107 questions
3
votes
1 answer

reference_wrapper does not print in cout, but reference_wrapper does?

Why the line where i am trying to print the "reference_wrapper for string" is giving error for unsupported operator<< for "reference_wrapper for string" but does not give on "reference_wrapper for int"? int main(){ int s= 43; string str =…
Gtrex
  • 247
  • 1
  • 7
3
votes
1 answer

Why deleting the referenced value by an element of a vector> does not invalidate the vector?

I found out the nice tool provided by std::reference_wrapper, but this behaviour sounds weird to me. #include #include #include #include #include #include int main() { …
Luca Jungla
  • 150
  • 8
3
votes
1 answer

std::find fails on a std::vector> with a "no match for ‘operator==’" error when T is in a namespace

I'm currently work on a large code project and wanted to take the opportunity to learn about and use namespaces. All of the classes I've defined reside within a single namespace, Test. One of my classes, referred to here as Thing, has a unique ID. I…
user9220358
3
votes
1 answer

Why is `std::reference_wrapper` deprecated in c++17 and removed in c++20?

Since C++11, std::reference_wrapper is a small "shim" template that is a class type that is constructible from and convertible to a reference type. It can be used in generic containers which might not otherwise support…
Chris Beck
  • 15,614
  • 4
  • 51
  • 87
3
votes
1 answer

compile error with std::reference_wrapper "has no member named" when used with class

So I've found this fancy stuff std::reference_wrapper in C++11 which I found could be useful in future program development. I played with it for a bit and I thought it could replace good-old reference in some cases for better usage, until I bumped…
3
votes
2 answers

How to compare and assign between std::vector and std::vector>?

Here are two different type of std::vector, as an example: std::vector> rv; std::vector v; A possible way to assign between them is: for (const auto& mc : rv) { v.push_back(mc.get()); } It works. But…
Ringo_D
  • 784
  • 5
  • 18
3
votes
1 answer

Implementation of std::reference_wrapper

While looking at the implementation of std::reference_wrapper here The constructors and operators are obvious to me but I didn't understand this part template< class... ArgTypes > typename std::result_of::type operator() (…
Laith
  • 1,248
  • 2
  • 11
  • 19
3
votes
2 answers

How to set an initial size for an STL container of reference_wrapper?

I have a vector of some reference type wrapped in reference_wrapper. Since I need to fill this container out of order I'm trying to set an initial size for the container: vector> v(5); v[3] = .. v[2] = .. v[4] = .. v[5] =…
none
  • 11,793
  • 9
  • 51
  • 87
3
votes
2 answers

How to avoid decay of std::reference_wrapper to plain references in std::make_tuple?

I' am trying to correctly produce a tuple of std::reference_wrappers to objects. By using the technique shown here I manage to map the std::ref function over all the original tuple, however, according to this (and it actually happens),…
tunnuz
  • 23,338
  • 31
  • 90
  • 128
2
votes
1 answer

C++ reference wrapper as function argument

From my understanding, reference wrapper is just a wrapper on reference, nothing too special about it. But why it is treated as the reference itself (rather than the wrapper) inside the function when passed as a function argument? #include…
yuhao
  • 71
  • 1
  • 5
2
votes
1 answer

Why does this usage of std::reference_wrapper not modify the original?

I have a problem with using the inbuilt & references in C++ correctly. I have an instance of a class named "Entity" it was created in the main() function. I also have an instance of a class named "Simulation" the Simulation class has a function…
2
votes
2 answers

Concept-restricted range-based for loop of std::list>

I have some class Foo and a std::list> and would like to iterate over its elements with a range-based for loop: #include #include #include class Foo { public: Foo(int a) : a(a) {} …
2
votes
1 answer

What is the crux of std::reference_wrapper implementation for the only purpose of makin std::ref work?

The example on the page of std::ref/std::cref shows the use of std::ref/std::cref to pass arguments to std::bind in a way that looks like std::bind is taking arguments by reference, when in reality it takes them all by value. Looking at that example…
Enlico
  • 23,259
  • 6
  • 48
  • 102
2
votes
1 answer

What is the difference between std::pair with references and reference wrappers

I've fallen over a bad assumption that I made. I discovered that std::pair is a thing. However I didn't expect the following to fail int a = 10; int b = 20; int c = 30; using PRef = std::pair; PRef p0 = {a,b}; PRef p1…
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
2
votes
1 answer

rvalue references, std::reference_wrappers and std::function

I was reading up on r-value references and move semantics. Experimenting this with std::function and std::reference_wrapper unfortunately confused me a bit more. #include #include #include #include…