Skip to main content
Filter by
Sorted by
Tagged with
2 votes
2 answers
194 views

Im wondering if there is a better, more typesafe, way to return "a selection" than I do at the moment. If we only ever have one selected element at a time, we can just return a reference, ...
darune's user avatar
  • 11.5k
2 votes
1 answer
201 views

A std::reference_wrapper is guaranteed to be initialized (but can be rebound, unlike a normal reference). Being a wrapper class, what's the reason for std::reference_wrapper not to overload operator-&...
darune's user avatar
  • 11.5k
0 votes
2 answers
98 views

I have multiple child classes derived of a parent class. I would like to resolve one of the static fields of one of the child classes, based on a parameter that is unique across the children through a ...
Gábor DANI's user avatar
  • 2,135
1 vote
1 answer
234 views

I'm trying to learn the use of std::reference_wrapper and std::ref and the use cases of it. Here is a sample code I wrote. Please help me solve the compilation error. And it would be great if you can ...
Aman Kumar's user avatar
0 votes
1 answer
107 views

Consider following snippet: #include <bits/stdc++.h> using namespace std; int main() { int x=3; int y=1; int z=2; cout<<(&x)<<' '<<(&y)<<' '<&...
Rupa's user avatar
  • 110
0 votes
2 answers
173 views

(I'm mostly interested in a c++17 solution, even though you see me using std::unwrap_ref_decay; just imagine I've copied and pasted the possible implementation in my C++17 code. On the other hand, I'm ...
Enlico's user avatar
  • 30.3k
1 vote
1 answer
114 views

I have a function that accepts N position vectors and updates them: using Vector3r = Eigen::Matrix<Real, 3, 1, Eigen::DontAlign>; template<unsigned int N> using Vectorr = Eigen::Matrix<...
Sin3point14's user avatar
2 votes
2 answers
117 views

Trying to create a reference wrapper similar to std::reference_wrapper, but with possibility to call the methods of the wrapped object. #include <iostream> class A { public: void f() { ...
Israel Yankelovich's user avatar
2 votes
1 answer
550 views

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 ...
Chris's user avatar
  • 23
1 vote
1 answer
1k views

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 ...
NeitherNor's user avatar
0 votes
2 answers
135 views

I have 2 classes lets say Class A and Class B, class A { public: A(B b); B GetB(); private: B b; }; class B { public: B(); void IncrementCounter(); int GetCounter(); ...
heyloo's user avatar
  • 37
0 votes
2 answers
107 views

I am working on my C++ home-project and got into trouble with the following C2676 error: binary '==': 'std::reference_wrapper<Interactable>' does not define this operator or a conversion to a ...
Sarungard's user avatar
3 votes
0 answers
483 views

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 ...
alfC's user avatar
  • 16.8k
3 votes
3 answers
237 views

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<int> variable, and then ...
Eunho Choi's user avatar
0 votes
0 answers
310 views

How to initialize a vector of reference_wrapper member variable in a class. I have tried to initialize with a vector in constructor initialisation_list. class Test { public: Test(std::...
Arun AC's user avatar
  • 417
8 votes
1 answer
1k views

I have been trying to understand the implementation of std::reference_wrapper, from here, which is as follows: namespace detail { template <class T> constexpr T& FUN(T& t) noexcept { ...
warrior_monk's user avatar
4 votes
1 answer
1k views

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 ...
yuhao's user avatar
  • 91
2 votes
1 answer
233 views

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 ...
Jan Ráček's user avatar
3 votes
2 answers
178 views

Recently, I've decided to write a class storing a variant with reference_wrapper<const vector> and vector for having a choice either to own the value or having only a reference of it. That is, ...
koleydoscope's user avatar
2 votes
2 answers
500 views

I have some class Foo and a std::list<std::reference_wrapper<Foo>> and would like to iterate over its elements with a range-based for loop: #include <list> #include <functional>...
Maximilian Keßler's user avatar
1 vote
1 answer
459 views

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

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 ...
Enlico's user avatar
  • 30.3k
3 votes
1 answer
498 views

I was trying out the std::reference_wrapper with the following snippet int a = 42, b = 52; std::tuple<std::reference_wrapper<int>> t = std::make_tuple(std::ref(a)); std::get<0>(t) = ...
YHC's user avatar
  • 88
0 votes
1 answer
272 views

I have a class like so: class A { std::vector< std::vector< std::pair< uint32_t, std::reference_wrapper<std::vector<uint8_t>>>>> ...
Fotiadis M.'s user avatar
2 votes
1 answer
1k views

I've fallen over a bad assumption that I made. I discovered that std::pair<int &, int &> is a thing. However I didn't expect the following to fail int a = 10; int b = 20; int c = 30; ...
bradgonesurfing's user avatar
2 votes
1 answer
336 views

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 ...
Shiwayari's user avatar
  • 355
1 vote
0 answers
41 views

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 ...
Evethir's user avatar
  • 41
0 votes
0 answers
64 views

I have been implementing Strassen algorithm with threads. I have passed data to threads by structures and launching them by function pthread_create() . The problem is I'm operating on std::vector &...
kjtn99's user avatar
  • 81
1 vote
1 answer
663 views

#include <vector> #include <array> int main() { typedef std::array<int,2> point; typedef std::vector<std::reference_wrapper<point>> route; std::vector<...
yushizhao's user avatar
  • 861
0 votes
1 answer
161 views

In the C++ 20 Standard the constructor of the class template std::reference_wrapper is a template. template<class U> constexpr reference_wrapper(U&&) noexcept(see below ); while the ...
Vlad from Moscow's user avatar
0 votes
1 answer
63 views

I'm using boost::variant to implement type erasure when sending data across network. When the data arrives to the client or server, I'm inspecting its structure to retrieve the underlying information ...
tsuki's user avatar
  • 907
0 votes
1 answer
237 views

I know that a std::ref(object) creates a std::reference_wrapper(object), and that std::reference_wrapper has a non-explicit type conversion operator member function operator T&() const and I ...
Luca's user avatar
  • 1,786
1 vote
2 answers
488 views

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 <...
Jason's user avatar
  • 386
3 votes
1 answer
293 views

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 ...
Gtrex's user avatar
  • 247
3 votes
1 answer
641 views

I found out the nice tool provided by std::reference_wrapper, but this behaviour sounds weird to me. #include <iostream> #include <string> #include <algorithm> #include <...
Luca Jungla's user avatar
1 vote
1 answer
1k views

It's my understanding that std::variant cannot directly hold references. However, std::reference_wrapper is a fully qualified type that can be put into things like std::vector, and since one can make ...
markt1964's user avatar
  • 2,896
4 votes
1 answer
281 views

I recently learnt that std::reference_wrapper<T> 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 ...
Albjenow's user avatar
  • 379
5 votes
1 answer
1k views

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 &...
sajas's user avatar
  • 1,609
4 votes
1 answer
738 views

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 ...
user avatar
0 votes
2 answers
462 views

I have the following class: class Data; class A { public: A(Data& _data) : data(_data) {} Data& getData() {return data;} const Data& getData() const {return data;} private: ...
Jakub Zaverka's user avatar
0 votes
2 answers
801 views

I have a number of inheritance-related types that I want to use from standard container (std::reference_wrapper is a proper value type for such a container, AFAIU). However, I do not understand, how ...
Student4K's user avatar
  • 960
0 votes
1 answer
1k views

I am trying to run an example with vector of reference wrappers, but run into compilation error at the vector variable declaration. Here is the code: #include <iostream> #include <vector> ...
Student4K's user avatar
  • 960
10 votes
2 answers
750 views

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::...
skypjack's user avatar
  • 50.9k
1 vote
2 answers
267 views

I present to you this code riddle: Using this compiler: user@bruh:~/test$ g++ --version g++ (Ubuntu 7.3.0-16ubuntu3) 7.3.0 Copyright (C) 2017 Free Software Foundation, Inc. This is free ...
MK-Ultra-'s user avatar
0 votes
2 answers
168 views

I'm new to C++11 and I'm confused about the usage of std::refence_wrapper class. let's consider the example shown in https://en.cppreference.com/w/cpp/utility/functional/reference_wrapper ,which ...
Marco Randazzo's user avatar
0 votes
1 answer
76 views

Consider following piece of code: //option no 1 struct foo{ foo(baz &b) : _b(b){} std::reference_wrapper<baz> _b; }; //option no 2 struct bar{ bar(std::reference_wrapper<...
bartop's user avatar
  • 10.5k
0 votes
1 answer
858 views

I am trying to build an unordered_map to vector variables which are members of my class. I can do this using standard pointers * but this requires the use of (*x) to access the vector. I wondered if ...
Simon Woodward's user avatar
1 vote
1 answer
2k views

I am trying to use std::reference_wrapper on all elements of another vector. But when I delete the original element, I want to automatically delete the element in the vector of references. I did some ...
user1000's user avatar
  • 101
2 votes
1 answer
555 views

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 <iostream> #include &...
xworder's user avatar
  • 111
3 votes
1 answer
3k views

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 ...
Chris Beck's user avatar
  • 16.4k