为什么我不能使用引用类型作为容器类型的值类型?

时间:2022-08-27 16:10:04

For example, std::vector<int&> vec_int; this seems to be invalid in c++. Why is this invalid?

例如,std::< int > vec_int向量;在c++中,这似乎是无效的。为什么这是无效的吗?

3 个解决方案

#1


3  

Answer, as per chryspi request above. As commented in other responses, you cannot use references directly as references don't exist by themselves.

回答,按照上面的chryspi请求。正如其他响应中的注释,不能直接使用引用,因为引用本身不存在。

You can use references, however, but by using the boost::reference_wrapper<T> utility class:

但是,您可以使用引用,但是通过使用boost:::reference_wrapper 实用工具类:

    typedef boost::reference_wrapper<int> intref;
    std::vector<intref> v;
    int i;
    i = 9;

    v.push_back (boost::ref (i));  // store &i

    int& j = v[0];
    j = 10;
    //v[0].get() = 10;

    std::cout << i << std::endl; // prints 10

I put as an example how to modify directly the element v[0]. Note that it is somewhat tricky (you have to call the get() method) because you get a boost::reference_wrapper<T> instead of an actual reference.

我以如何直接修改元素v[0]为例。注意,这有点棘手(您必须调用get()方法),因为您会得到一个boost::reference_wrapper 而不是实际的引用。

For storing pointers safely with respect to memory, you can use boost::shared_ptr similarly.

为了安全地将指针存储在内存中,可以使用boost::shared_ptr。

#2


9  

STL containers need to be able to construct objects with default constructor. You cannot do that with a reference. A reference is guaranteed to be valid, therefore it always has to be initialized with assignment.

STL容器需要能够使用默认构造函数构造对象。你不能用引用来做。一个引用被保证是有效的,因此它总是必须被赋值初始化。

You need to use a pointer instead.

你需要使用一个指针。

#3


3  

Internally, a vector<T> uses an array to store a sequence of T objects. Since references aren't objects, there is no such thing as an array of references (see 8.3.2 §5 in the standard), thus reference types cannot be used to parameterize the vector template.

在内部,向量 使用一个数组来存储T对象的序列。因为没有对象的引用,没有所谓的数组的引用(参见8.3.2§5标准),因此不能使用引用类型参数化向量模板。

What you probably want is a vector of smart pointers such as std::vector<boost::shared_ptr<T> > or a dedicated pointer container such as boost::ptr_vector<T>.

您可能需要一个智能指针的向量,比如std::vector >或一个专用指针容器,如boost::ptr_vector

#1


3  

Answer, as per chryspi request above. As commented in other responses, you cannot use references directly as references don't exist by themselves.

回答,按照上面的chryspi请求。正如其他响应中的注释,不能直接使用引用,因为引用本身不存在。

You can use references, however, but by using the boost::reference_wrapper<T> utility class:

但是,您可以使用引用,但是通过使用boost:::reference_wrapper 实用工具类:

    typedef boost::reference_wrapper<int> intref;
    std::vector<intref> v;
    int i;
    i = 9;

    v.push_back (boost::ref (i));  // store &i

    int& j = v[0];
    j = 10;
    //v[0].get() = 10;

    std::cout << i << std::endl; // prints 10

I put as an example how to modify directly the element v[0]. Note that it is somewhat tricky (you have to call the get() method) because you get a boost::reference_wrapper<T> instead of an actual reference.

我以如何直接修改元素v[0]为例。注意,这有点棘手(您必须调用get()方法),因为您会得到一个boost::reference_wrapper 而不是实际的引用。

For storing pointers safely with respect to memory, you can use boost::shared_ptr similarly.

为了安全地将指针存储在内存中,可以使用boost::shared_ptr。

#2


9  

STL containers need to be able to construct objects with default constructor. You cannot do that with a reference. A reference is guaranteed to be valid, therefore it always has to be initialized with assignment.

STL容器需要能够使用默认构造函数构造对象。你不能用引用来做。一个引用被保证是有效的,因此它总是必须被赋值初始化。

You need to use a pointer instead.

你需要使用一个指针。

#3


3  

Internally, a vector<T> uses an array to store a sequence of T objects. Since references aren't objects, there is no such thing as an array of references (see 8.3.2 §5 in the standard), thus reference types cannot be used to parameterize the vector template.

在内部,向量 使用一个数组来存储T对象的序列。因为没有对象的引用,没有所谓的数组的引用(参见8.3.2§5标准),因此不能使用引用类型参数化向量模板。

What you probably want is a vector of smart pointers such as std::vector<boost::shared_ptr<T> > or a dedicated pointer container such as boost::ptr_vector<T>.

您可能需要一个智能指针的向量,比如std::vector >或一个专用指针容器,如boost::ptr_vector