Qt 5 and C++11

  • Hi everyone,

    I have a class for which it is convenient for me to use the C++11 "delegating constructors" feature, so I thought I'd try it.

    I'm using Qt 5.1 (on a laptop with Ubuntu 13.04), the compiler is gcc 4.7.3 (I hope I got this right).

    When I compile, I get the warning "delegating constructors only available with -std=c++11 or -std=gnu++11 [enabled by default]". It's only a warning though, not an error, so does this mean it works anyway? How do I get rid of the warning?

    Following suggestions I read here and there on the web, I tried adding

    @CONFIG += c++11@


    @QMAKE_CXXFLAGS += -std=c++11@

    in the .pro file of the project. But when I do that, I get 74 errors upon compilation , all of which seem to be happening in the file qobjectsdef_impl.h.

    Anyone know what's going on? Of course, I don't really need to use C++11 features, I'm just asking in case there is a simple fix. Thanks!

  • Moderators

    At least for the MinGW on windows you can use "6.58.10 Diagnostic pragmas" to switch off the warnings. However, I believe that is a brand-new feature of gcc 4.8. Nevertheless, you might want to check.

  • That doesn't really help, but thanks anyway

  • Anyone else?

  • Moderators

    Did you clean your -source- build tree and re-run qmake after editing your .pro file? What are those errors exactly?

  • Hi JKSH and thanks for trying to help.

    I hadn't taken a close look at the error (my bad, sorry), but it seems that the problem might be due to the line:
    @#include <QColor>@
    in one of my header files. Of course, this might simply be the first inclusion of a Qt class, I don't know.

    I thought a screenshot might be useful for you to see what the error look like:

    NB : "link":https://drive.google.com/uc?id=0B41pJbehW4QbSjNsVVNvcmsyanM to the image, is it shows too small here

  • Update: I upgraded to gcc 4.8.1 and Qt 5.1.2, which I compiled from the sources. And I still get the errors! What's going on?

  • Moderators


    Your code uses a complex type. Is that std::complex? You could have a name clash with the "I" template parameter in qobjectdefs_impl.h ("I" also represents the imaginary unit).

    Get rid of "using namespace std" in tools.h and h2isometry.h (and all the headers that they include) and try again.

  • Wow, you rock! Problem solved!

    The problem was that I had written somewhere (in types.h, included by tools.h) #define I complex(0.0, 1.0). As you said, there was probably a name clash in qobjectdefs_impl.h. I don't know how you got that from what I posted!

    NB: I defined complex later in that file by typedef std::complex<double> complex;. (now I don't even understand how things worked in the first place, I suppose I should have written #define I std::complex<double>(0.0, 1.0)). The one good point that I get is, I didn't write any using namespace std ;)

    Anyway, for some weird reason that line #define I complex(0.0, 1.0) didn't cause any problems as long as I didn't write CONFIG += c++11 in the pro file, but not it did after writing it.

    A great many thanks JKSH, I'm very glad.

  • Moderators

    I'm glad that's resolved :)

    I had help -- someone brought up your issue in the Qt "mailing list":http://lists.qt-project.org/pipermail/interest/2013-August/008264.html and someone else did a detailed analysis on your screenshot. Since you didn't need to write "std::", I assumed that you used "using namespace std" so that "I" was included in your global namespace. Both of us were slightly off though -- I just found out that "I" is defined in C but not C++.

    In your screenshot, line 484 is a preprocessor "#else", attached to "#ifndef Q_COMPILER_VARIADIC_TEMPLATES" around line 127. The code below line 484 uses "I", but the code above does not. The code below line 484 is used only if variadic templates are supported (i.e. only if C++11 is enabled). The code above line 484 is used otherwise.

  • I see! I had indeed created a question on stackoverflow ("here":http://stackoverflow.com/questions/18243995/qt-5-and-c11-qobjectdefs-impl-h-wont-compile/) when it looked like not much was going on here. Then someone (Greenflow) asked on the Qt mailing list.

    When you read the mail, it's funny (and impressive) how the analysis is carried based on the poor information of a stupid screenshot , it looks like archaeological work or something. Anyway, great team work!

    By the way, I don't know if you have an opinion on this, but what should I do instead of #define I std::complex<double> (0.0, 1.0) ?

  • Moderators

    In general, I wouldn't use #define for constant values/objects. In your example, the preprocessor would insert std::complex<double> (0.0, 1.0) everywhere you write I, which means your program would construct a new copy of the object everywhere you write I.

    Since you want a constant value, make I a constant global variable:
    // types.h
    const std::complex<double> I(0.0, 1.0);

  • Understood, thanks!

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.