std :: vector< Foo>当Foo的一些成员是引用 [英] std::vector<Foo> when some members of Foo are references

查看:166
本文介绍了std :: vector< Foo>当Foo的一些成员是引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常喜欢尽可能使用引用而不是指针,它使我的意见中的语法更清晰。在这种情况下,我有一个类:

I often prefer to use references than pointers whenever possible, it makes the syntax cleaner in my opinion. In this case, I have a class:

class Foo
{
public:
    Foo(Bar & bar) : bar_(bar) {}

private:
    Bar & bar_;
};

这个类的编译器,因为一旦一个引用被设置,它不能被改变(我可以在技术上定义我自己的不改变bar_,但这不会是必需的行为,所以我宁愿编译器抱怨如果我尝试分配一个foo)。

operator=() is implicitely deleted by the compiler for such a class, since once a reference is set, it cannot be changed (I can technically define my own that doesn't change bar_, but this would not be the required behaviour, so I'd rather the compiler complain if I try to assign a foo).

我需要的是一个 std :: vector< Foo> v; 。这在C ++ 11之前是不可能的,因为模板参数必须是CopyAssignable。实际上,当我调用 v.push_back(Foo(bar)); 时,编译器抱怨。但我觉得这可能是因为C ++ 11和Move语义。

What I need is a std::vector<Foo> v;. This is impossible before C++11, since the template parameter must to be CopyAssignable. Indeed, when I call v.push_back(Foo(bar));, the compiler complains. But I feel it could be possible since C++11 and Move semantics.

我的问题是:有一个解决方法使用C + + 11,向量可能,或者我困在这种情况下,没有办法使用指针,而不是引用?如果有解决方法,我非常感谢代码段,因为我不熟悉move语义。

My question is: is there a workaround using C++11 that would make building such a vector possible, or am I stuck in this case and have no way around using pointers instead of references? If there's a workaround, I'd highly appreciate a code snippet since I'm unfamiliar with move semantics.

推荐答案

Voila emplace_back 能够做工作,因为完美的转发。

Voila emplace_back is able to do the job because of perfect forwarding.

#include <vector>                                                                  

class Bar{};                                                                       
class Foo                                                                          
{                                                                                  
public:                                                                            
    Foo(Bar & bar) : bar_(bar) {}                                                  

private:                                                                           
    Bar & bar_;                                                                    
};                                                                                 
using namespace std;                                                               
int main() {                                                                       
    vector<Foo> v;                                                                 
    Bar bar;
    v.emplace_back(bar);                                                     
} 

您也可以在一个容器中存储一个引用, std :: reference_wrapper used like std :: vector< std :: reference_wrapper< int>> v

You can also store a reference by itself in a container with std::reference_wrapper used likestd::vector<std::reference_wrapper<int>> v

这篇关于std :: vector&lt; Foo&gt;当Foo的一些成员是引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆