119 questions
2 votes
2 answers
194 views
How to return a list of selected elements of some underlying container in a typesafe manner?
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, ...
2 votes
1 answer
201 views
What's the reason for std::reference_wrapper to not overload operator->?
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-&...
0 votes
2 answers
98 views
How to assign value to a static map of reference_wrappers?
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 ...
1 vote
1 answer
234 views
How do I use std::reference_wrapper to store the reference in an std::unordered_map?
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 ...
0 votes
1 answer
107 views
reference_wrapper in c++ containers
Consider following snippet: #include <bits/stdc++.h> using namespace std; int main() { int x=3; int y=1; int z=2; cout<<(&x)<<' '<<(&y)<<' '<&...
0 votes
2 answers
174 views
How to make tag_invoke CPO deal uniformly with types matching a meta-predicate/concept and with those types wrapped in reference_wrapper?
(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 ...
1 vote
1 answer
114 views
Generalising a C++ templated function so it can accept std::reference_wrapper or non-wrapped type of N elements
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<...
2 votes
2 answers
117 views
Call member function that can be reached by implicit conversion in C++
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() { ...
2 votes
1 answer
550 views
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 ...
1 vote
1 answer
1k views
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 ...
0 votes
2 answers
135 views
How to save an object inside another object in C++
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(); ...
0 votes
2 answers
107 views
C2676 error with reference_wrapper and custom class
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 ...
3 votes
0 answers
483 views
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 ...
3 votes
3 answers
237 views
Why does reference_wrapper<string> comparison not work? [duplicate]
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 ...
0 votes
0 answers
310 views
how to initialize a vector member variable of reference_wrapper inside a class?
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::...