PDA

View Full Version : Bug/Issue: compiler is confused with calling simple arg(int, int, int, QChar) func?!



sonysan
27th May 2015, 10:46
Hello @all,

guess, what I get as output if I try to run this simple code?!

QString test_str = QString("20%1").arg(5, 2, 10, '0');
qDebug("%s", qPrintable(test_str));

if I use MingW4.8.3 and Qt5.3 on win7 64bit, then I got compiler warnings:
Warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second: [enabled by default]
QString test_str = QString("20%1").arg(5, 2, 10, '0');
^

But if I modify it to:

QString test_str = QString("20%1").arg(5, 2, 10, QChar('0');
then it works fine.
I am trying to understand, and your help is welcome!

Best Regards!

ChrisW67
27th May 2015, 12:25
You get that when you try to compile the code because this:


QString test_str = QString("20%1").arg(5, 2, 10, '0');

matches several possibilities (which your compiler listed for you). For example:


QString arg(double a, int fieldWidth = 0, char format = 'g', int precision = -1, QChar fillChar = QLatin1Char( ' ' )) const
QString arg(short int a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char( ' ' )) const

5 can be implicitly cast to double or short int to fit either prototype.
2 is an int and matches both perfectly.
10 is an int but can be implicitly cast to char to fit the other prototype. It is an invalid format letter but the compiler cannot know that.
'0' is a char but can be implicitly cast to either int or QChar to match either prototype.
The last argument in the first prototype has a default.
So, your call could match either prototype and the compiler warns that C++ standard says these are ambiguous. However, the compiler has chosen one of the options using a heuristic (the double variant on my machine, probably because cast of an int to short might lose information) but it may not be the one you expected.

Specifying QChar('0') as the last argument eliminates the first option (a QChar cannot be cast to int).