Questions tagged [reference-wrapper]

107 questions
145
votes
4 answers

What is the difference between std::reference_wrapper and a simple pointer?

Why is there a need to have std::reference_wrapper? Where should it be used? How is it different from a simple pointer? How its performance compares to a simple pointer?
Laurynas Lazauskas
  • 2,855
  • 2
  • 21
  • 26
18
votes
2 answers

Why can template instances not be deduced in `std::reference_wrapper`s?

Suppose I have some object of type T, and I want to put it into a reference wrapper: int a = 5, b = 7; std::reference_wrapper p(a), q(b); // or "auto p = std::ref(a)" Now I can readily say if (p < q), because the reference wrapper has a…
18
votes
2 answers

Issue with std::reference_wrapper

The issue is clear with the following code: #include #include #include int main() { //std::vector a, b; int a = 0, b = 0; auto refa = std::ref(a); auto refb = std::ref(b); std::cout << (refa < refb)…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
16
votes
4 answers

Why does std::reference_wrapper not accept a temporary?

Normally, rvalues can bind to const references (const SomeType&). It's built into the language. However, std::reference_wrapper does not accept an rvalue as its constructor argument since the corresponding overload is deliberately deleted.…
isarandi
  • 3,120
  • 25
  • 35
14
votes
1 answer

Why standard containers use function templates instead of non-template Koenig operators

This question is inspired by Issue with std::reference_wrapper. Let' say, for example, operator< for std::vector. It's defined as a function template as template< class T, class Alloc > bool operator<( const vector& lhs, …
Lingxi
  • 14,579
  • 2
  • 37
  • 93
11
votes
1 answer

Constant reference_wrapper

In the following code, the aim is to have a reference_wrapper b such that when a changes, b also changes however, the opposite should not be allowed that is, a should not change when b changes. I tried two ways: Lines 7 and 8. Line 7 caused…
Shibli
  • 5,879
  • 13
  • 62
  • 126
11
votes
3 answers

Why does std::make_tuple turn std::reference_wrapper arguments into X&?

In the C++11 standard it states that (see cppreference.com, see also section 20.4.2.4 of the standard) it states that template< class... Types > tuple make_tuple( Types&&... args ); Creates a tuple object, deducing the target type from…
Ralph Tandetzky
  • 22,780
  • 11
  • 73
  • 120
10
votes
1 answer

Use std::reference_wrapper in std::map

I thought maps and reference_wrappers would go easy but I tripped over something weird: #include #include int main(void) { std::map> mb; const int a = 5; mb[0] =…
WorldSEnder
  • 4,875
  • 2
  • 28
  • 64
9
votes
2 answers

Is there any guarantee on the size of an std::reference_wrapper?

Consider the case in which I've enough storage to host a void * and therefore to construct in-place a pointer. Is there any guarantee that the same storage is big enough to always store also an std::reference_wrapper? Kind of (written out of my…
skypjack
  • 49,335
  • 19
  • 95
  • 187
9
votes
1 answer

Should std::reference_wrapper contain the default comparator "<" operator?

The STL uses the "less than" as the default comparator. An STL comparator call on an object wrapped with the reference_wrapper<> does not compile, even if the underlying class has the "<" operator defined. It seems, this is because there is no…
VSOverFlow
  • 1,025
  • 8
  • 11
8
votes
2 answers

Ambiguous constructor taking std::reference_wrapper when compiling with -pedantic

I have a class with both a copy constructor and a constructor taking a std::reference_wrapper: #include #include class Class { public: Class() { std::cout << "Class()" << std::endl; } Class(Class const &)…
zennehoy
  • 6,405
  • 28
  • 55
7
votes
1 answer

std::reference_wrapper, constructor implementation explaination

I have been trying to understand the implementation of std::reference_wrapper, from here, which is as follows: namespace detail { template constexpr T& FUN(T& t) noexcept { return t; } template void FUN(T&&) = delete; } …
warrior_monk
  • 383
  • 2
  • 14
7
votes
2 answers

Why can't I sort a vector of reference_wrapper?

I want a sorted view of a std::vector but I don't want to modify the original container. std::reference_wrapper seems perfect for this and it works just fine for a vector of integers. I've created this small…
Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
7
votes
2 answers

How to convert std::vector > to std::vector

I have a local std::vector > and now I want to return a real copy of its elements (i.e std::vector). Is there a better way than a loop? Example: std::vector foobar() { std::vector >…
Dmytrii S.
  • 231
  • 2
  • 4
  • 11
6
votes
1 answer

std::optional> - is it OK?

Is std::optional> is conforming to the Standard (or draft) of C++17? The Standard explicitly says, that std::optional for a reference type is ill-formed. But does it include reference_wrapper?
vladon
  • 8,158
  • 2
  • 47
  • 91
1
2 3 4 5 6 7 8