-
Notifications
You must be signed in to change notification settings - Fork 617
auto casting #130
Comments
You want to make those const:
Or you compiler might decide some really strange casts in some cases where there are temporaries. Next, I recommend against it, implicit casts to integer types (including bool) from objects is very easy to go wrong. |
Not had a problem so far - but thanks for that ! What do you mean by " implicit casts to integer types (including bool) from objects is very easy to go wrong." ? I've found that if you try to read the wrong type, you'll just get a zero. |
Well, compilers can decide to implicitly cast without you expecting it. I think this example works unexpectedly, as both Foo instances are cast to bool in the if(), and then the compare is true.
You can have the same thing if you compare objects that are incompatible for comparison, or if the comparison operator isn't defined const while the casts are (at this example also includes a temporary) |
I would definitely recommend defining the conversion functions as |
I added the following to json11.hpp
It makes using json11 much more convenient as the compiler will auto cast your variables for you, eg:
int id = myJson["id"];
The text was updated successfully, but these errors were encountered: