Questions tagged [reference-wrapper]

107 questions
0
votes
1 answer

Transform vector of reference wrapper to Base class to vector of reference_wrapper to Derived class cannot dynamic cast

As in title. I got this error when trying to dynamic_cast cannot dynamic_cast '(& obj)->std::reference_wrapper<_Tp>::get()' (of type 'class MEPObject') to type 'class MEPGene&' (target is not pointer or reference to complete type) …
user3191398
0
votes
1 answer

Transform std:vector> to std:vector> Runtime error time: 0 memory: 3412 signal:6

I am trying to convert vector of references to Base objects to vector of references to Derived objects. Everything is compiling fine, but i got this error: Runtime error time: 0 memory: 3412 signal:6 This is my code: #include #include…
user3191398
0
votes
0 answers

Is this a good use of reference_wrapper?

From what I understand, the C++ template class reference_wrapper is useful for references in containers and tuples, because it essentially provides a copy-constructor and assignment operator to normal references. In other words, it seems to be like…
Caetano Sauer
  • 263
  • 1
  • 10
0
votes
1 answer

Polymorphism in C++ with a vector with base classes and reference_wrapper

I need to use polymorphism in C++ for objects which are stored in a vector. Understand from other questions that the objects in the vector need to be stored by reference since otherwise object slicing occurs. My understanding is that…
dparnas
  • 4,090
  • 4
  • 33
  • 52
0
votes
1 answer

How should I convert a container to a container of reference_wrappers?

Question: Assume a function void doFoo(const std::vector >& data); where DataT is some type holding some data. I used std::vector just as the typical example of a container class. What would you say is the most elegant…
user2296653
  • 1,151
  • 1
  • 8
  • 17
0
votes
1 answer

Where's my bad_alloc coming from?

I have this code. scores is a field in HiscoreTable of type std::vector> void HiscoreTable::replaceRecord(Record rec) { bool found = false; for (reference_wrapper r : scores) { if (r.get().name == rec.name)…
Matt G
  • 1,661
  • 19
  • 32
0
votes
2 answers

Cast pointer to reference_wrapper to pointer to underlying?

Here's my situation: I'm given data as a pointer say double*. I want to wrap this in a vector to use a library, and to avoid messing around with pointers. Not wanting to copy the entire array, I use a vector of reference wrappers. If I then want to…
retrocookie
  • 319
  • 2
  • 10
0
votes
5 answers

reference_wrapper Referencing Primitive

I was under the impression that I could use reference_wrapper to generate a functor that would return the object passed into the reference_wrapper ctor. But this isn't working. Am I doing it wrong? If so is there a better way to accomplish this? I…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
0
votes
0 answers

Semantics for wrapped objects: reference/value by default via std::move/std::ref

In recent times I am using often a natural idiom I "discovered" in C++11 that is that wrapped object can automatically hold reference when this is possible. The main question here will be about the comparison with the behavior of this "idiom" to…
alfC
  • 14,261
  • 4
  • 67
  • 118
-1
votes
1 answer

What is the correct way to pass a std::vector to be a parameter?

I want to pass the a vector filled with object based same class. Both std::reference_wrapper and no-reference do not fix it. What's the correct way to solve this? class MyClass { public: MyClass(int){}; virtual void print() const { …
Ringo_D
  • 784
  • 5
  • 18
-1
votes
1 answer

Return C++ reference wrapper

Is it dangerous to return a reference wrapper like showed below: std::vector> foo() { int x = 10; std::vector> vec; vec.push_back(x); return vec; } foo2() { std::cout <<…
user1056903
  • 921
  • 9
  • 26
-1
votes
2 answers

Change of address after re-allocation

In the code below, whenever a new member is inserted to the std::vectorA, due to memory re-allocations, std::vector>B points to a wrong address. Is it possible to make the referencing vector to track the re-allocations…
Shibli
  • 5,879
  • 13
  • 62
  • 126
-1
votes
2 answers

Is there anything like a std::value_wrapper parallel to std::reference_wrapper?

(UPDATE: This question stems from an implementation of a wrapper class passed by value for an object that has different meanings for const Foo and Foo, a move based entirely on strong opinions from people here. Prior, I'd been passing around const…
-2
votes
1 answer

std::function from std::bind initialization in the initializer list

I have a map of actions to be taken upon certain choice, struct option { int num; std::string txt; std::function action; }; void funct_with_params(int &a, int &b) { a = 3; b = 4; } int param1 = 1; int param2 = 3; I want to…
cerkiewny
  • 2,761
  • 18
  • 36
-2
votes
1 answer

reference_wrapper implementation details

How does this templated operator() work in reference_wrapper implementation template class reference_wrapper { public: // types typedef T type; // construct/copy/destroy reference_wrapper(T& ref) noexcept :…
barney
  • 2,172
  • 1
  • 16
  • 25