``Unsigned is evil'' ... Robert H.
class VariantValue { public: VariantValue(bool val); VariantValue(int val); VariantValue(float val); VariantValue(double val); VariantValue(std::string val); };What will be the problem? What happens if you construct a VariantValue like as follows:
VariantValue val0("Test");2006-12-24(Sun)
VariantValue val0(std::string("Test"));However, it is not. There is a conversion from (const char*) to bool. Or C does not really have bool. If it is 0, then false otherwise true. I personally do not like to write a pointer is not NULL as
assert(ptr);This is valid, but the lack of the real boolean type is not so nice for me. I can not compare the value with true. It is quite hard language that a comparison with true is dangerous. For example, 12345 is true as in if expression but if you compare with
true
, it fails.
explicit
. All of these
constructor should write as the following:
class VariantValue { public: explicit VariantValue(bool val); explicit VariantValue(int val); explicit VariantValue(float val); explicit VariantValue(double val); explicit VariantValue(std::string val); };See also C++NINNIN FIXME.
int x = -1000; unsigned int y = 10; float a = x + y; std::cout << "x + y = " << x << " + " << y << " = " << a << std::endl; x = -10; y = 1000; a = x + y; std::cout << "x + y = " << x << " + " << y << " = " << a << std::endl;The result of this is:
x + y = -1000 + 10 = 4.29497e+09 x + y = -10 + 1000 = 990Because (int) will be promoted to (unsigned int), if the minus value is shown up in the middle of the computation, it breaks your intuition.