-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversion operators not considered #369
Comments
Conceptually, I think the problem is that |
I think this may be related to the fact that |
@nlohmann I'm a bit late to respond, sorry. I don't agree with your statement, here's an example to illustrate: void foo(bool);
void foo(int);
struct Test {};
Test t;
foo(t); // error: Test can't be converted to bool or int Nothing surprising here, but once you add If built-in types can be added to json I don't see why types that are convertible to builtins shouldn't be? @TurpentineDistillery I don't think that's true, the problems seems to be the templated assignment operator that will match the exact type being assigned and not consider any conversions. Assigning an int selects a different overload. |
@popizdeh What do you think of my initial comment? I still do not see how a compiler should find out how a |
I don't know how easy this would be, I might look into it a bit more when I get some spare time, but in a nutshell we need to change the template assignment operator because it's always a better match. If json supports ints then it needs to support all types that are convertible to int. We just need to find a way to write that assignment operator differently so that implicit conversions are taken into account. |
So far, for each value type (array, object, string, boolean, floating-point number, unsigned integer number, and signed integer number), we have a constructor like template<class CompatibleStringType, typename std::enable_if<
std::is_constructible<string_t, CompatibleStringType>::value, int>::type = 0>
basic_json(const CompatibleStringType& val)
: basic_json(string_t(val))
{
assert_invariant();
}
I am not sure how an extension to implicit conversions would look like, and whether it would help rather than add new ambiguities. |
Any ideas on this? |
I had a quick look, here's what I have so far. Adding a templated assignment operator gets us a bit closer to what I had in mind. template <typename T>
reference operator=(T t)
{
*this = basic_json{t};
return *this;
} The problem with this is that it only works for classes whose conversion operator type exactly matches one of basic_json's constructor parameter types. So having a type that implicitly converts to |
Any ideas how to proceed here? |
You could add a Maybe there's a way to do it via a free function? For example, add a SFINAE constructor to basic_json that accepts any type T where the call "to_json(the_t_value)" works? |
I guess I could add an operator to my class, or I'll just live with static_cast. I don't think it's worth going overboard for this. It'd be nice to have but I was hoping there'd be a simple solution. Feel free to close this Niels, I'll send you a patch if I get a better solution. |
Maybe #328 will be a solution for this. Good that you don't mind closing this. I you find a solution, I'm all ears. |
I do think #423 will be the solution, I just added tests with implicit conversions, and it seems to work well. EDIT: You will need a |
This doesn't compile, I was hoping it would use the conversion operator.
The text was updated successfully, but these errors were encountered: