Probably it's cause i didn't sleep all the night...but can someone explain me why the first operator[] (the const one) generates a warning in MSVC 2010 saying that i'm returning the address of a temorary variable, and the second operator[] (the non-const one) produces a compiler error stating: 'return' : cannot convert from 'std::_Vb_reference<_Alloc>' to 'bool &' ?!?!?
Thanks a lot to everyone will waste time on my question.
class ReqAtts
{
public:
const bool& operator[](size_t ii) const
{
return _atts[ii];
}
bool& operator[](size_t ii)
{
return _atts[ii];
}
private:
std::vector<bool> _atts;
};
std::vector<bool>
is space efficient in most implementations (g++, MSVC): every item may be stored as a single bit, not as bool. You can read more here: en.cppreference.com
You have 2 options:
std::vector<unsigned char>
(or other integer type) instead of std::vector<bool>
.Use std::vector<bool>::reference
as return type for operator[]
as follows:
class ReqAtts
{
public:
std::vector<bool>::const_reference operator[](size_t ii) const
{
return _atts[ii];
}
std::vector<bool>::reference operator[](size_t ii)
{
return _atts[ii];
}
private:
std::vector<bool> _atts;
};
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加