Weird overload function situation with QT/C++ types

RuiDo

I'm having a weird problem with an overloaded function one of my work colleagues made, but we're both C++/QT newbies and can't figure out the reason for such a situation.

The situation is as follows:

We have an overloaded function:

foo(bool arg1 = false);
foo(const QVariant &arg1, const QString &arg2 = NULL, bool arg3 = false);

On the first one we have only one optional bool argument passed as value; On the second we have one QVariant reference, an optional qstring reference and a bool.

What happens on runtime is the when I call for example:

foo("some_c++_string");

instead of using the 2nd one and casting the string to QVariant, it uses the first one and probably ignores the argument! The weird thing is that it doesn't even complain about not existing an overload for foo(char *) on compile time for example! But if we do:

 foo(QString("some_qt_string"));

it goes to the second overload as expected.

So, the question is: Why in the world does it decide to go for an overload which accepts one optional argument not even of the same type instead of using the second one and trying to use the string argument as a QVariant?

It probably has something to do with passing the arguments as reference and giving them default values, but I've tried several combinations and always went wrong.

Thanks for your time

Angew is no longer proud of SO

That's because of the ordering of implicit conversions. Converting a string literal to a bool requires only a standard conversion sequence: an array-to-pointer conversion (obtaining const char*) and a boolean conversion (obtaining bool).

On the other hand, converting a string literal to a QVariant requires a user-defined conversion sequence, since it involves a class (QVariant).

And per C++11 13.3.3.2/2,

When comparing the basic forms of implicit conversion sequences (as defined in 13.3.3.1)

  • a standard conversion sequence (13.3.3.1.1) is a better conversion sequence than a user-defined conversion sequence or an ellipsis conversion sequence

Which means that the first overload is strictly better and is thus chosen.


I'd wager you're building with Visual Studio, by the way. Otherwise, the construct

foo(QString("some_qt_string"));

wouldn't even compile. That's because QString("some_qt_string") creates a temporary object, and in standard C++, temporaries cannot bind to non-const lvalue references (which your second overload takes). However, Visual Studio allows that as an extension.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Weird overload resolution with variadic function templates

From Dev

Can't overload functions with union of function types

From Dev

Overload Templated Function for All String Types

From Dev

Unary operators "++" and "--" weird situation

From Dev

Unary operators "++" and "--" weird situation

From Dev

How to overload function for different iterator value_types in C++

From Dev

Overload resolution with method references and function interface specializations for primitive types

From Dev

Function not found from global namespace if there's an overload with different argument types

From Dev

when to free resource - weird situation

From Dev

Weird situation when returning char *

From Dev

Weird extension method overload resolution

From Dev

Overload using Enum types

From Dev

Weird situation in prime number checking code

From Dev

Weird NULL situation in Laravel Eloquent models

From Dev

PHP use() Function In This Situation

From Dev

How to NOT overload the std::map::at member function in the case where the two template types are the same?

From Dev

mypy error, overload with Union/Optional, "Overloaded function signatures 1 and 2 overlap with incompatible return types"

From Dev

return type of overload operator + exhibit weird behaviour

From Dev

return type of overload operator + exhibit weird behaviour

From Dev

Overload addAll function in Java

From Dev

Overload resolution with std::function

From Dev

Overload resolution for pointer to function

From Java

Overload a lambda function

From Dev

Template function overload resolution

From Dev

Generic function overload

From Dev

Function template overload resolution

From Dev

Function template overload puzzle

From Dev

Calling another function overload

From Dev

Val Function and overload notice

Related Related

  1. 1

    Weird overload resolution with variadic function templates

  2. 2

    Can't overload functions with union of function types

  3. 3

    Overload Templated Function for All String Types

  4. 4

    Unary operators "++" and "--" weird situation

  5. 5

    Unary operators "++" and "--" weird situation

  6. 6

    How to overload function for different iterator value_types in C++

  7. 7

    Overload resolution with method references and function interface specializations for primitive types

  8. 8

    Function not found from global namespace if there's an overload with different argument types

  9. 9

    when to free resource - weird situation

  10. 10

    Weird situation when returning char *

  11. 11

    Weird extension method overload resolution

  12. 12

    Overload using Enum types

  13. 13

    Weird situation in prime number checking code

  14. 14

    Weird NULL situation in Laravel Eloquent models

  15. 15

    PHP use() Function In This Situation

  16. 16

    How to NOT overload the std::map::at member function in the case where the two template types are the same?

  17. 17

    mypy error, overload with Union/Optional, "Overloaded function signatures 1 and 2 overlap with incompatible return types"

  18. 18

    return type of overload operator + exhibit weird behaviour

  19. 19

    return type of overload operator + exhibit weird behaviour

  20. 20

    Overload addAll function in Java

  21. 21

    Overload resolution with std::function

  22. 22

    Overload resolution for pointer to function

  23. 23

    Overload a lambda function

  24. 24

    Template function overload resolution

  25. 25

    Generic function overload

  26. 26

    Function template overload resolution

  27. 27

    Function template overload puzzle

  28. 28

    Calling another function overload

  29. 29

    Val Function and overload notice

HotTag

Archive