0

Possible Duplicate:
Why vector<bool>::reference doesn’t return reference to bool?

I have a mock class which I am using in my unit tests.

I looks something like:

class Mock
{
public:
    ...

    static ReturnType isKeyFunction(bool* _isKey)
    {
        _isKey = &(*isKeyI++);
        ...
    }
    ...
    static std::vector<bool> isKey;
    static std::vector<bool>::iterator isKeyI;
};

So the vector is filled with values and the iterator is incremented each time a value is used by the test.

The problem is that the code wont compile and I'm guessing its because std::vector is saved as bits and I can point to a member in it with a bool*.

error C2440: '=' : cannot convert from 'std::_Vb_reference<_Alloc> *' to 'bool *'

How might I solve this?

Community
  • 1
  • 1
Baz
  • 12,713
  • 38
  • 145
  • 268
  • 1
    Use `std::vector` instead. – john Nov 08 '12 at 12:00
  • 1
    `vector` is "special". – Luchian Grigore Nov 08 '12 at 12:01
  • http://en.wikipedia.org/wiki/Sequence_container_%28C%2B%2B%29#Specialization_for_bool – leftaroundabout Nov 08 '12 at 12:03
  • Even if `vector` behaved normally, it doesn't make sense to assign the `_isKey` parameter like you do. – interjay Nov 08 '12 at 12:07
  • @interjay Why not? I need to point to a value within the vector. – Baz Nov 08 '12 at 12:20
  • Because it will overwrite whatever value is passed to the function for the `_isKey` parameter, so it might as well be a local variable. If your intent is for the caller to see the new value of `_isKey` then this won't work. – interjay Nov 08 '12 at 12:26
  • You can use `struct bool_type { bool value; bool_type(bool v) : value(v) { ; } bool * operator & () { return &value; } operator bool & () { return value; } };` and `template< typename T > using B = std::conditional_t< std::is_same< T, bool >{}, bool_type, T >;` to filter the actual type. – Tomilov Anatoliy Sep 09 '15 at 07:37

0 Answers0