Results 1 to 2 of 2

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

  1. #1
    Join Date
    May 2015
    Posts
    1
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

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

    Hello @all,

    guess, what I get as output if I try to run this simple code?!
    Qt Code:
    1. QString test_str = QString("20%1").arg(5, 2, 10, '0');
    2. qDebug("%s", qPrintable(test_str));
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. QString test_str = QString("20%1").arg(5, 2, 10, QChar('0');
    To copy to clipboard, switch view to plain text mode 
    then it works fine.
    I am trying to understand, and your help is welcome!

    Best Regards!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Bug/Issue: compiler is confused with calling simple arg(int, int, int, QChar) fun

    You get that when you try to compile the code because this:
    Qt Code:
    1. QString test_str = QString("20%1").arg(5, 2, 10, '0');
    To copy to clipboard, switch view to plain text mode 
    matches several possibilities (which your compiler listed for you). For example:
    Qt Code:
    1. QString arg(double a, int fieldWidth = 0, char format = 'g', int precision = -1, QChar fillChar = QLatin1Char( ' ' )) const
    2. QString arg(short int a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char( ' ' )) const
    To copy to clipboard, switch view to plain text mode 
    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).

  3. The following user says thank you to ChrisW67 for this useful post:

    sonysan (27th May 2015)

Similar Threads

  1. compiler error in calling friend function!!??
    By aurora in forum Qt Programming
    Replies: 11
    Last Post: 15th November 2011, 21:25
  2. Qdbus remote method calling issue
    By yuvaraj.addu in forum Qt Programming
    Replies: 1
    Last Post: 21st May 2011, 00:28
  3. Compiler error when calling QObject::connect. What am I missing?
    By themanwiththequestion in forum Qt Programming
    Replies: 4
    Last Post: 2nd February 2010, 14:33
  4. setTiles() func of QCanvas
    By Kapil in forum Newbie
    Replies: 1
    Last Post: 25th March 2006, 19:13
  5. where's the sleep() func?
    By TheKedge in forum Qt Programming
    Replies: 11
    Last Post: 2nd February 2006, 15:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.