119 questions
2
votes
2
answers
192
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, ...
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 ...
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)<<' '<&...
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
1
answer
547
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 ...
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();
...
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 ...
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::...
4
votes
1
answer
1k
views
C++ reference wrapper as function argument
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 ...
3
votes
2
answers
176
views
Best viable overloaded function between std::reference_wrapper<const T> and T
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, ...
1
vote
1
answer
459
views
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 ...
3
votes
1
answer
496
views
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<std::reference_wrapper<int>> t = std::make_tuple(std::ref(a));
std::get<0>(t) = ...
2
votes
1
answer
1k
views
What is the difference between std::pair with references and reference wrappers [duplicate]
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;
...
1
vote
0
answers
41
views
Use raw reference or std::reference_wrapper [duplicate]
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 ...
1
vote
1
answer
659
views
reference_wrapper cause "incomplete type is not allowed"
#include <vector>
#include <array>
int main() {
typedef std::array<int,2> point;
typedef std::vector<std::reference_wrapper<point>> route;
std::vector<...
0
votes
1
answer
61
views
const correctness for custom reference wrapper
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 ...
1
vote
2
answers
488
views
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 <...
3
votes
1
answer
640
views
Why deleting the referenced value by an element of a vector<reference_wrapper<T>> does not invalidate the vector?
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 <...
4
votes
1
answer
280
views
Subscript operator for reference_wrapper
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 ...
4
votes
1
answer
735
views
std::find fails on a std::vector<std::reference_wrapper<T>> with a "no match for ‘operator==’" error when T is in a namespace
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 ...
0
votes
2
answers
801
views
Initializing vector with reference_wrapper's locally
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 ...
10
votes
2
answers
748
views
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::...
0
votes
2
answers
168
views
how to obtain a C array from an std::vector<std::reference_wrapper>
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 ...
0
votes
1
answer
856
views
Accessing class members using reference_wrapper instead of standard pointers in C++11
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 ...
2
votes
1
answer
553
views
rvalue references, std::reference_wrappers and std::function
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 &...