An array of references is illegal because a reference is not an object. According to the C++ standard, an object is a region of storage, and it is not specified if a reference needs storage (Standard §11.3. 2/4). Thus, sizeof does not return the size of a reference, but the size of the referred object.
std::reference_wrapper is a class template that wraps a reference in a copyable, assignable object. It is frequently used as a mechanism to store references inside standard containers (like std::vector) which cannot normally hold references.
vector :: assign() in C++ STL vector:: assign() is an STL in C++ which assigns new values to the vector elements by replacing old ones. It can also modify the size of the vector if necessary.
An ordinary vector encountered in C++ programming, is a vector of objects of the same type. These objects can be fundamental objects or objects instantiated from a class. This article illustrates examples of vector of pointers, to same object type.
The component type of containers like vectors must be assignable. References are not assignable (you can only initialize them once when they are declared, and you cannot make them reference something else later). Other non-assignable types are also not allowed as components of containers, e.g. vector<const int>
is not allowed.
yes you can, look for std::reference_wrapper
, that mimics a reference but is assignable and also can be "reseated"
By their very nature, references can only be set at the time they are created; i.e., the following two lines have very different effects:
int & A = B; // makes A an alias for B
A = C; // assigns value of C to B.
Futher, this is illegal:
int & D; // must be set to a int variable.
However, when you create a vector, there is no way to assign values to it's items at creation. You are essentially just making a whole bunch of the last example.
Ion Todirel already mentioned an answer YES using std::reference_wrapper
. Since C++11 we have a mechanism to retrieve object from std::vector
and remove the reference by using std::remove_reference
. Below is given an example compiled using g++
and clang
with option-std=c++11
and executed successfully.
#include <iostream>
#include <vector>
#include<functional>
class MyClass {
public:
void func() {
std::cout << "I am func \n";
}
MyClass(int y) : x(y) {}
int getval()
{
return x;
}
private:
int x;
};
int main() {
std::vector<std::reference_wrapper<MyClass>> vec;
MyClass obj1(2);
MyClass obj2(3);
MyClass& obj_ref1 = std::ref(obj1);
MyClass& obj_ref2 = obj2;
vec.push_back(obj_ref1);
vec.push_back(obj_ref2);
for (auto obj3 : vec)
{
std::remove_reference<MyClass&>::type(obj3).func();
std::cout << std::remove_reference<MyClass&>::type(obj3).getval() << "\n";
}
}
Use std::reference_wrapper
like this:
#include <functional>
#include <string>
#include <vector>
#include <iostream>
int main()
{
std::string hello = "Hello, ";
std::string world = "everyone!";
typedef std::vector<std::reference_wrapper<std::string>> vec_t;
vec_t vec = {hello, world};
vec[1].get() = "world!";
std::cout << hello << world << std::endl;
return 0;
}
Demo
As standard suggests, for a standard container X
containing objects of type T
, T
must be Erasable
from X
.
Erasable
means that the following expression is well formed:
allocator_traits<A>::destroy(m, p)
A
is container's allocator type, m
is allocator instance and p
is a pointer of type *T
. See here for Erasable
definition.
By default, std::allocator<T>
is used as vector's allocator. With the default allocator, the requirement is equivalent to the validity of p->~T()
(Note the T
is a reference type and p
is pointer to a reference). However, pointer to a reference is illegal, hence the expression is not well formed.
It's a flaw in the C++ language. You can't take the address of a reference, since attempting to do so would result in the address of the object being referred to, and thus you can never get a pointer to a reference. std::vector
works with pointers to its elements, so the values being stored need to be able to be pointed to. You'll have to use pointers instead.
boost::ptr_vector<int>
will work.
Edit: was a suggestion to use std::vector< boost::ref<int> >
, which will not work because you can't default-construct a boost::ref
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With