F
Fred Zwarts
In my software I need a vector of boolean values.
My first attempt was to use vector<bool>, but vector has a specialization for the bool type
with unwanted implications. For example, I need to pass elements of the vector as
reference to bool (bool&) to certain functions which may modify the value of such elements.
This is not possible with vector<bool>.
My next idea was to create a class Boolean_t which can be used instead of the bool type,
with transparent functionality. So I attempted the following:
class Boolean_t {
private:
bool Flag;
public:
// Normal constructor:
Boolean_t (const bool InitialValue = false) : Flag (InitialValue) {}
// Define conversion to bool and reference to bool.
operator bool () const {
return Flag;
}
operator bool & () {
return Flag;
}
};
My problem is with two operator definitions. I need both.
The first one is needed to convert a const object to a bool value,
which cannot be done with the second one.
The second one is needed to pass objects of type Boolean_t to functions that need a reference to bool,
which cannot be done with the first one.
However, if an object of type Boolean_t is used in a boolean expression, the compiler complains
about an ambiguity in the reference to a conversion function. Indeed, I understand that both
of these conversion functions could be used in this case.
Is there a solution to overcome this ambiguity and to keep the functionality?
My first attempt was to use vector<bool>, but vector has a specialization for the bool type
with unwanted implications. For example, I need to pass elements of the vector as
reference to bool (bool&) to certain functions which may modify the value of such elements.
This is not possible with vector<bool>.
My next idea was to create a class Boolean_t which can be used instead of the bool type,
with transparent functionality. So I attempted the following:
class Boolean_t {
private:
bool Flag;
public:
// Normal constructor:
Boolean_t (const bool InitialValue = false) : Flag (InitialValue) {}
// Define conversion to bool and reference to bool.
operator bool () const {
return Flag;
}
operator bool & () {
return Flag;
}
};
My problem is with two operator definitions. I need both.
The first one is needed to convert a const object to a bool value,
which cannot be done with the second one.
The second one is needed to pass objects of type Boolean_t to functions that need a reference to bool,
which cannot be done with the first one.
However, if an object of type Boolean_t is used in a boolean expression, the compiler complains
about an ambiguity in the reference to a conversion function. Indeed, I understand that both
of these conversion functions could be used in this case.
Is there a solution to overcome this ambiguity and to keep the functionality?