Why can't I make a vector of references?

前端 未结 9 586
梦如初夏
梦如初夏 2020-11-22 03:32

When I do this:

std::vector hello;

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



        
相关标签:
9条回答
  • 2020-11-22 03:57

    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.

    0 讨论(0)
  • 2020-11-22 03:57

    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.

    0 讨论(0)
  • 2020-11-22 03:58

    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.

    0 讨论(0)
  • 2020-11-22 04:08

    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.

    0 讨论(0)
  • 2020-11-22 04:09

    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.

    0 讨论(0)
  • 2020-11-22 04:12

    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!

    0 讨论(0)
提交回复
热议问题