Why can't I make a vector of references?
Asked Answered
C

11

451

When I do this:

std::vector<int> hello;

Everything works great. However, when I make it a vector of references instead:

std::vector<int &> hello;

I get horrible errors like

error C2528: 'pointer' : pointer to reference is illegal

I want to put a bunch of references to structs into a vector, so that I don't have to meddle with pointers. Why is vector throwing a tantrum about this? Is my only option to use a vector of pointers instead?

Coterminous answered 28/5, 2009 at 18:4 Comment(2)
you can use std::vector<reference_wrapper<int> > hello; See informit.com/guides/content.aspx?g=cplusplus&seqNum=217Greenhaw
@amit the link is valid no longer, official documentation hereSancha
T
431

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.

Thalassa answered 28/5, 2009 at 18:25 Comment(12)
Are you saying I can't have a vector of vectors? (I'm sure I've done that...)Acervate
Yes, a std::vector< std::vector<int> > is correct, std::vector is assignable.Gamma
Indeed, this is the "actual" reason. The error about T* being impossible of T is U& is just a side-effect of the violated requirement that T must be assignable. If vector was able to precisely check the type parameter, then it would probably say "violated requirement: T& not assignable"Kidding
Checking the assignable concept at boost.org/doc/libs/1_39_0/doc/html/Assignable.html all operations except the swap are valid on references.Greenhaw
I think the component type must also be default-constructible, no? And references are not that either.Chengtu
@cdhowie, no, the component type need not be default-constructible, as long as you don't call resize() or the vector constructor that takes a size. (I conclude this from trying it using g++, not from reading any spec)Tinkling
I had this issue trying to do: vector<vector <int>&> The solution was to work with: vector<vector<int>*>Filter
@JohannesSchaub-litb I guess it could do like static_assert<std::is_same_v<T, std::remove_reference<T>>> or something along those linesOberland
This is no longer true. Since C++11, the only operation-independent requirement for element is to be "Erasable", and reference is not. See #33144919.Divulgate
References are not assignable (you can only initialize them once when they are declared, and you cannot make them reference something else later). Then why can I for (item& i: myVector) i = foo; ? I am assigning the reference to something else.Morehouse
@johnbakers: You are modifying the thing the reference points to, not the reference itself. Modifying the reference itself would allow you to have the reference to point to something else, which is impossible with C++ references.Thalassa
It could, of course, be a specialization that internally stores pointers and presents them as references in the public API. That's what reference_wrapper does.Faille
C
157

yes you can, look for std::reference_wrapper, that mimics a reference but is assignable and also can be "reseated"

Capriccio answered 20/11, 2012 at 5:8 Comment(5)
Is there a way of getting around calling get() first when trying to access a method of an instance of a class in this wrapper? E.g. reference_wrapper<MyClass> my_ref(...); my_ref.get().doStuff(); is not very reference like.Ballot
Isn't it implcitly castable to the Type itself by returning the reference?Doubletalk
Yes, but that requires a context that implies which conversion is required. Member access does not do that, hence the need for .get(). What timdiels wants is operator.; have a look at the latest proposals/discussions about that.Gestalt
Note however that while reference_wrapper unlike the raw reference type can be "reseated", it still cannot be uninitialized, so it is not default constructible. That means that while one can construct vector<reference_wrapper<int>>{int1, int2, int3}, one still cannot have a vector with with default constructed elements: vector<reference_wrapper<int>>(5). This isn't even caught by IDE (CLion), but fails compilation.Lauryn
I find it odd that this is declared in <functional> -- it seems more general than merely callable objects.Oconner
A
43

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.

Acervate answered 28/5, 2009 at 18:21 Comment(4)
"when you create a vector, there is no way to assign values to it's items at creation" I don't understand what you mean by this statement. What are "its items at creation"? I can create an empty vector. And I can add items with .push_back(). You are just pointing out that references are not default-constructible. But I can definitely have vectors of classes that are not default-constructible.Thalassa
The element ype of std::vector<T> is not required to be default constructible. You can write struct A { A(int); private: A(); }; vector<A> a; just fine - as long as you don't use such methods that require it to be default constructible (like v.resize(100); - but instead you will need to do v.resize(100, A(1)); )Kidding
And how would you write such a push_back() in this case? It will still use assignment, not construction.Acervate
James Curran, No default construction takes place there. push_back just placement-news A into the preallocated buffer. See here: #672852 . Note that my claim is only that vector can handle non-default-constructible types. I don't claim, of course, that it could handle T& (it can't, of course).Kidding
W
37

TL; DR

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

Long answer

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.

Workout answered 22/3, 2019 at 13:58 Comment(0)
C
35

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";
        }             
    }
Cota answered 29/5, 2015 at 3:35 Comment(2)
I don’t see the value in std::remove_reference<> here. The point of std::remove_reference<> is to allow you to write "the type T, but without being a reference if it is one". So std::remove_reference<MyClass&>::type is just the same as writing MyClass.Mercury
No value at all in that - you can just write for (MyClass obj3 : vec) std::cout << obj3.getval() << "\n"; (or for (const MyClass& obj3: vec) if you declare getval() const, as you should).Vinegarette
N
20

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.

Nonalcoholic answered 28/5, 2009 at 18:7 Comment(5)
I guess it could be implemented using a void * buffer and placement new. Not that this would make much sense.Safford
"Flaw in the language" is too strong. It is by design. I don't think it is required that vector works with pointers to elements. However it is required that the element be assignable. References are not.Rivero
You can't take the sizeof a reference either.Blond
you don't need a pointer to a reference, you have the reference, if you do need the pointer, just keep the pointer itself; there is no problem to be solved hereCapriccio
It could, of course, be a specialization that internally stores pointers and presents them as references in the public API. That's what reference_wrapper does.Faille
R
15

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.

Rosenarosenbaum answered 28/5, 2009 at 18:27 Comment(3)
But you can have a vector or non default-constructible types, right? You only have to be careful not to use the default ctor. of the vectorTl
@Manuel: Or resize.Rodmun
Be careful, Boost's pointer containers take exclusive ownership of the pointees. Quote: "When you do need shared semantics, this library is not what you need."Jarlathus
G
3

As other have mentioned, you will probably end up using a vector of pointers instead.

However, you may want to consider using a ptr_vector instead!

Gamma answered 28/5, 2009 at 18:30 Comment(2)
This answer is not workable, since a ptr_vector is supposed to be a storage. That is it will delete the pointers at removal. So it is not usable for his purpose.Subgenus
One way or another you have to store something somewhere! Docs: "Instances of std::reference_wrapper are objects (they can be copied or stored in containers)"Thrive
D
1

I found the reason in C++14, "International Standard ISO/IEC 14882:2014(E) Programming Language C++"

[8.3.2-5.s1] There shall be no references to references, no arrays of references, and no pointers to references.

Driedup answered 23/6, 2022 at 12:35 Comment(0)
F
0

There is no technical reason why you couldn't have a vector of references, it's just not a part of the API, presumably to keep it similar to an array. A specialization could easily be added that works with references, stores them as pointers internally and presents as references in the API:

vector<int&> ivref;
int i = 42;
ivref.push_back(i); // address of i is stored
ivref.front() = 43; // i is set to 43
ivref.push_back(44); // Marked deleted for rvalue references 
ivref.resize(10);    // Marked deleted for vector of references
Faille answered 29/12, 2022 at 15:32 Comment(0)
H
-1

As the other comments suggest, you are confined to using pointers. But if it helps, here is one technique to avoid facing directly with pointers.

You can do something like the following:

vector<int*> iarray;
int default_item = 0; // for handling out-of-range exception

int& get_item_as_ref(unsigned int idx) {
   // handling out-of-range exception
   if(idx >= iarray.size()) 
      return default_item;
   return reinterpret_cast<int&>(*iarray[idx]);
}
Hardened answered 24/5, 2017 at 10:20 Comment(2)
reinterpret_cast is not neededPetrosal
As the other answers show, we are not confined to using pointers at all.Gestalt

© 2022 - 2024 — McMap. All rights reserved.