Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-38810

QFlags type resolution (unexpectedly resolved as int)

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • Not Evaluated
    • None
    • 4.8.6, 5.2.1
    • Core: Other
    • None
    • archlinux/gcc/32bit

    Description

      This review
      https://git.reviewboard.kde.org/r/118016/

      triggered an interesting question, since this code:

      #include <stdio.h>
      #include <QFlags>
      
      enum Foo { Foo1 = 1, Foo2 = 2};
      enum Bar { Bar1 = 1, Bar2 = 2};
      
      Q_DECLARE_FLAGS(Foos, Foo)
      Q_DECLARE_OPERATORS_FOR_FLAGS(Foos)
      Q_DECLARE_FLAGS(Bars, Bar)
      Q_DECLARE_OPERATORS_FOR_FLAGS(Bars)
      
      void fooBar(Foos foo, Bars bar, int i = 5)
      {
          printf("foo bar int\n");
      }
      
      void fooBar(Foos foo, int i = 5)
      {
          printf("foo int\n");
      }
      
      int main(int, char **)
      {
          fooBar(Foo1, Bar1);
          fooBar(Foo1, Bar1|Bar2);
      }
      
      
      g++ -fPIC `pkg-config --libs --cflags Qt5Core` -o enum_cast enum_cast.cpp
      g++ `pkg-config --libs --cflags QtCore` -o enum_cast enum_cast.cpp
      

      produces this output:

         foo int
         foo bar int
      

      on Qt4 & Qt5 (g++ (GCC) 4.8.2 20140206 (prerelease))

      Ie. the or'd Flag is resolved as "Bars", but the simple Flag is resolved as int

      That seems unexpectable to me - it should be

         foo bar int
         foo bar int
      

      Notice that

      • the Q_DECLARE_OPERATORS_FOR_FLAGS macro does not trigger the behavior, it's part of QFlags (w/o you'd get "foo int" twice)
      • using "dumb" enums will get you the (expectable) output
           foo bar int
           foo int  
        

      There's https://bugreports.qt-project.org/browse/QTBUG-13577 which seems related was closed invalid, since flags have to be power of two, but that is not the issue here (1, 2 are 2^x|x=0,1 - I had 0,1 before, what should according to the deco still be fine, but causes the same behavior)

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            thiago Thiago Macieira
            luebking Thomas Lübking
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes