Questions tagged [reference-wrapper]
107 questions
2
votes
2 answers
How to copy references of elements of one vector to another vector?
I have a vector:
std::vector sea;
And now I want to hold pointers to all elements of this vector in another vector. But just for a personal exercise I want to do this in a fancy C++ style, so instead of std::vector p_sea I…
user4385532
2
votes
1 answer
Incorrect understanding of implicit conversion from reference to std::reference_wrapper or compiler bug?
I'm confused about some code involving references and std::reference_wrapper and I'm not clear at all whether this is my fault since I'm misunderstanding how reference wrapper works or if I hit a compiler error.
I have a simple map, pairing a…

Alvaro Palma Aste
- 381
- 3
- 16
2
votes
3 answers
C++: reference wrappers and printf
I have a std::vector of std::reference_wrapper objects that I want to print with printf (without cout); now, if I write
int a=5;
std::reference_wrapper b=a;
printf("%i\n\n",b);
I get a nosense number (I think is the address of a); to obtain my…

volperossa
- 1,339
- 20
- 33
2
votes
1 answer
Type emulating a C++ reference better than std::reference_wrapper
I am designing a class that behaves like a C++ references but does some extra instrumental stuff (bookkeeping).
Initially I thought that std::reference_wrapper would be a good model. But after a while I realized that std::reference_wrapper…

alfC
- 14,261
- 4
- 67
- 118
2
votes
2 answers
Const correctness with reference_wrapper
In various situations I have a collection (e.g. vector) of objects that needs to be processed by a number of functions. Some of the functions need to modify the objects while others don't. The objects' classes may inherit from an abstract base…

tsnorri
- 1,966
- 5
- 21
- 29
2
votes
3 answers
Pass vector > to vector?
I have a large std::vector a, but I would like to work only on a subset of it. The idea was to create a std::vector > refa that only contains the said subset (in the mwe, all elements 1< a < 4). I would then like to pass…

Jost
- 410
- 7
- 19
2
votes
1 answer
Accessing reference wrapper elements in vector c++11
In Graph class:
typedef std::pair PIRV;
In another class that uses graph:
typedef std::priority_queue>, compareEdge> PECMP;
Now I am trying to access the first element in the…

smac89
- 39,374
- 15
- 132
- 179
2
votes
2 answers
C++ How does an std::reference_wrapper implicitly convert to reference?
I've recently begun using the std::reference_wrapper class. When replacing certain uses of primitive references, I noticed that I did not have to use the get() function to pass the reference_wrappers as parameters to functions that take a normal…

Elliot Hatch
- 1,038
- 1
- 12
- 26
1
vote
1 answer
What happens to a std:.reference_wrapper if the reference used to create it goes out of scope?
What happens to a std::reference_wrapper if the reference used to create it goes out of scope?
Can it still still provide access to the underlying object (which would still exist),
or would it be dangling and attempts to use it would result in UB?
I…

Chris
- 13
- 1
- 3
1
vote
1 answer
std::expected, references, and std::reference_wrapper
This question is related to std::expected and reference return type
I try to get my head around std::expected (respectively https://github.com/TartanLlama/expected) as an alternative error handling methodology, and I like the concept. However,…

NeitherNor
- 254
- 2
- 7
1
vote
1 answer
Problems sorting an array of std::reference_wrapper, referencing vectors
I am a bit stuck on this, I'm using a std::array to store reference wrappers to vectors. I was trying to sort these by the vector's size using std::sort but am unable for reasons I am not quite sure of, even after reading the compiler errors. Will I…

VevsVire
- 15
- 1
- 3
1
vote
1 answer
vector .. things going out of scope? how does it work?
Use case: I am converting data from a very old program of mine to a database friendly format. There are parts where I have to do multiple passes over the old data, because in particular the keys have to first exist before I can reference them in…

Shiwayari
- 315
- 3
- 12
1
vote
0 answers
Use raw reference or std::reference_wrapper
I've noticed the implementation of std::reference_wrapper in C++11, however, I'm wondering why I should use the std::reference_wrapper instead of a "raw" reference variable.
I've understood that a reference wrapper is an object that can be copied…

Evethir
- 31
- 3
1
vote
1 answer
reference_wrapper cause "incomplete type is not allowed"
#include
#include
int main() {
typedef std::array point;
typedef std::vector> route;
std::vector> lattice = {
{point{ {0,0} },point{ {0,1} }},
…

yushizhao
- 67
- 1
- 7
1
vote
2 answers
C++ reference_wrapper vector fills with wrong values
I'm trying to use reference wrappers in C++, since I want to be able to directly alter objects' values, but their values are all wrong. Here's an example that demonstrates this problem:
#include
#include
#include…

Jason
- 302
- 2
- 10