Questions tagged [reference-wrapper]

107 questions
6
votes
1 answer

How to detect std::reference_wrapper in C++ at compile time

Lets say we have some variadic template and need to treat std::reference_wrapper parameters differently. How can we achieve that?
Sergei Krivonos
  • 4,217
  • 3
  • 39
  • 54
6
votes
1 answer

Why reference_wrapper behaves differently for built-in types?

I have the following use of std::reference_wrapper for a build in type (double) and for a user defined type (std::string). Why do they behave differently in the case of the stream operator? #include //reference…
alfC
  • 14,261
  • 4
  • 67
  • 118
6
votes
2 answers

Comparing addresses of reference_wrappers

How can I compare two std::reference_wrappers by the references they hold? I want to see if the references of two std::reference_wrappers are equal or not. Edit: Sorry from confusion. I meant how to get the addresses of referents and compare them.
Shibli
  • 5,879
  • 13
  • 62
  • 126
5
votes
1 answer

Best way to treat std::reference_wrapper as std::reference_wrapper

I have two classes, say 'Base' and 'Derived', where Derived class inherits Base class. Then a container of references to Derived class instances (std::vector< std::reference_wrapper< Derived > > myContainer ). And finally, I have a function that…
sajas
  • 1,599
  • 1
  • 17
  • 39
5
votes
3 answers

Reserve a vector of reference_wrapper objects, how it is possible?

std::reference_wrapper isn't default constructable. So I can't write any of: std::vector> vec(10); std::array, 3> arr; However, and to my surprise, you can invoke the std::vector::reserve…
101010
  • 41,839
  • 11
  • 94
  • 168
5
votes
1 answer

When is std::reference_wrapper converted to T&?

Consider the following code: #include #include using namespace std; template void fun(T t) { t+=8; } int main() { int i = 0; fun(ref(i)); cout << i << endl; } This code prints "8". I assume…
oz1cz
  • 5,504
  • 6
  • 38
  • 58
4
votes
1 answer

Subscript operator for reference_wrapper

I recently learnt that std::reference_wrapper has an overload for the the function call operator in case T is function-like. I wonder whether there is a reason given by the standard committee to not include an array subscript operator in cases…
Albjenow
  • 369
  • 2
  • 12
4
votes
2 answers

Initialize an array of reference_wrapper

Coming from C to C++, I'm trying to understand the world of smart pointers & references. I've got the following: class Game { public: ... private: ... static GamePiece EmptyPiece; reference_wrapper _board[N][M] = {…
galah92
  • 3,621
  • 2
  • 29
  • 55
4
votes
1 answer

unordered_map value assignation when value type is reference_wrapper

I am experimenting with std::reference_wrapper. I don't understand why the following does work with a std::vector but not for a std::unordered_map: #include #include #include using namespace std; int main() { …
valentin
  • 2,596
  • 6
  • 28
  • 48
4
votes
0 answers

Default constructor with std::reference_wrapper as member?

I have this struct: struct MyStruct { MyStruct(const Wrapper &wrapper, /*...*/) : wrapper(std::cref(wrapper)), /*...*/ {} std::reference_wrapper wrapper; /*...*/ }; However, now I want to use a…
justHelloWorld
  • 6,478
  • 8
  • 58
  • 138
4
votes
1 answer

Overloading operator== for const std::reference_wrapper in std::unordered_map

I can't figure out how to get a std::string reference into an std::unordered_map using a std::reference_wrapper. Per the following link I understand I need to overload operator==. Why can template instances not be deduced in…
dontarius
  • 99
  • 1
  • 8
3
votes
0 answers

Why does reference_wrapper implement operator() in particular?

std::reference_wrapper is a standard library that wraps a reference in a copyable, assignable object. My view is that it has a weird design and seems like a hack to trick classes that store objects by value to believe that they are copying a value…
alfC
  • 14,261
  • 4
  • 67
  • 118
3
votes
3 answers

Why does reference_wrapper comparison not work?

int main() { int x = 1; auto ref = std::ref(x); if (x < ref) { ... } } In the above code, I made an int variable and a reference_wrapper variable, and then compared them, and it works well. However, If I change…
Eunho Choi
  • 167
  • 6
3
votes
2 answers

Best viable overloaded function between std::reference_wrapper and T

Recently, I've decided to write a class storing a variant with reference_wrapper and vector for having a choice either to own the value or having only a reference of it. That is, std::variant, reference_wrapper
3
votes
1 answer

std::reference_wrapper v.s. int&

I was trying out the std::reference_wrapper with the following snippet int a = 42, b = 52; std::tuple> t = std::make_tuple(std::ref(a)); std::get<0>(t) = b; std::cout << "t[0] = " << std::get<0>(t) << ", a = " << a << ",…
hsiu
  • 88
  • 5