You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
apf::parameter_map params;
params.set("x", 3.14);
double good = params.get("x", 7.0);
double bad = params.get("x", 7);
The value of good will be 3.14.
The value of bad will be 7.0 because the get() function template will be instantiated for the int type deduced from the default argument 7, the contained value 3.14 will fail to be converted to int, therefore the default value 7 will be returned, which will get converted to 7.0 on assignment to the double variable.
I think it would be safer to disable the automatic type deduction, but this would mean that many uses of get() would have to be updated with an explicit template parameter like this:
auto no_problem = params.get<double>("x", 7);
The value of no_problem would then be 3.14, because the function template will be instantiated explicitly for the double type and the conversion of the stored value 3.14 would succeed. The default value 7 would be converted to double before calling the function, but it wouldn't be needed in this case.
I think this would be much safer than the status quo and it would avoid bugs like in the link given at the top, but I'm not sure if forcing an explicit template parameter is the best solution.
Any other suggestions?
The text was updated successfully, but these errors were encountered:
This was discovered there: #88 (comment)
Example:
The value of
good
will be3.14
.The value of
bad
will be7.0
because theget()
function template will be instantiated for theint
type deduced from the default argument7
, the contained value3.14
will fail to be converted toint
, therefore the default value7
will be returned, which will get converted to7.0
on assignment to thedouble
variable.I think it would be safer to disable the automatic type deduction, but this would mean that many uses of
get()
would have to be updated with an explicit template parameter like this:The value of
no_problem
would then be3.14
, because the function template will be instantiated explicitly for thedouble
type and the conversion of the stored value3.14
would succeed. The default value7
would be converted todouble
before calling the function, but it wouldn't be needed in this case.I think this would be much safer than the status quo and it would avoid bugs like in the link given at the top, but I'm not sure if forcing an explicit template parameter is the best solution.
Any other suggestions?
The text was updated successfully, but these errors were encountered: