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
173
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::...
8
votes
1
answer
1k
views
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 <class T> constexpr T& FUN(T& t) noexcept { ...
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 ...
2
votes
1
answer
233
views
Why does this usage of std::reference_wrapper not modify the original?
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 ...
3
votes
2
answers
178
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, ...
2
votes
2
answers
500
views
Concept-restricted range-based for loop of std::list<std::reference_wrapper<T>>
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>...
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 ...
2
votes
1
answer
563
views
What is the crux of std::reference_wrapper implementation for the only purpose of makin std::ref work?
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 ...
3
votes
1
answer
498
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) = ...
0
votes
1
answer
272
views
How to correctly resize a complex vector?
I have a class like so:
class A {
std::vector<
std::vector<
std::pair<
uint32_t, std::reference_wrapper<std::vector<uint8_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;
...
2
votes
1
answer
336
views
vector<reference_wrapper> .. things going out of scope? how does it work?
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 ...
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 ...
0
votes
0
answers
64
views
Using std::ref, content does not change after exiting function
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 &...
1
vote
1
answer
663
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
161
views
non-template std::reference_wrapper assignment operator and template constructor
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 ...
0
votes
1
answer
63
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 ...
0
votes
1
answer
237
views
std::ref implicit conversion to reference confusion
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 ...
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
293
views
reference_wrapper<string> does not print in cout, but reference_wrapper<int> does?
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 ...
3
votes
1
answer
641
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 <...
1
vote
1
answer
1k
views
std variant and forward declarations
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 ...
4
votes
1
answer
281
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 ...
5
votes
1
answer
1k
views
Best way to treat std::reference_wrapper<Derived> as std::reference_wrapper<Base>
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 &...
4
votes
1
answer
738
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
462
views
Const correctness of STL library with reference-wrapper?
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:
...
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 ...
0
votes
1
answer
1k
views
Initializing vector with reference_wrappers
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>
...
10
votes
2
answers
750
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::...
1
vote
2
answers
267
views
runtime polymorphic invocation of pure virtual function via std::reference_wrapper behaving inconsistently
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 ...
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
76
views
How to pass data to reference wrapper
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<...
0
votes
1
answer
858
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 ...
1
vote
1
answer
2k
views
Design concerning std::reference_wrapper removals
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 ...
2
votes
1
answer
555
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 &...
3
votes
1
answer
3k
views
Why is `std::reference_wrapper` deprecated in c++17 and removed in c++20?
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 ...