取得临时地址,同时访问向量< bool>中的元素的地址。 [英] taking address of temporary while accessing address of a element in vector<bool>

查看:117
本文介绍了取得临时地址,同时访问向量< bool>中的元素的地址。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到警告只有当访问bool的向量中的元素的地址。
对于其他数据类型的向量,如int i没有得到任何警告。

I am getting the warning only while accessing address of element in vector of bool. For vector of other data types like int i don't get any warning.

例如

vector<bool> boolVect;
boolVect.push_back(false);
if (boolVect.size() > 0) {
    cout << &boolVect[0] << endl;
}   

在语句cout < ;& boolVect [0]<< endl;


有人可以澄清吗?

I get warning "taking address of temporary" at statement "cout << &boolVect[0] << endl;"
Can someone please clarify?

推荐答案

std ::向量< bool> 已损坏(请参阅 http:// www.informit.com/guides/content.aspx?g=cplusplus&seqNum=98 可选择vector< bool> )。它是 std :: vector< T> 的特殊化,但是各个元素被存储为打包位。因此,您不能获取单个元素的地址。因此,这真的很烦人。

std::vector<bool> is broken (see e.g. http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=98 or Alternative to vector<bool>). It's a specialization of std::vector<T>, but the individual elements are stored as packed bits. Therefore, you can't take the address of an individual element. Therefore, it's really annoying.

这篇关于取得临时地址,同时访问向量&lt; bool&gt;中的元素的地址。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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