How I can prevent the last line of this code from compiling?
#include <boost/optional.hpp>
int main()
{
typedef boost::optional<int> int_opt;
int_opt opt = 0;
bool x = opt; // <- I do not want this to compile
}
The last line doesn't examine opt
's contained int value, but instead compiles as a type conversion to bool, and doesn't seem to be what the user intended.
The safe bool idiom seems to be relevant here?
boost::optional
implementsoperator!
and this is why the last line above compiles. You can't even derive from this to hide that operator - you need to fix your code. – Capps