Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

c++11 error message when compiling c++14



  • I am using literals from c++14 in my code:

    // c++11
    constexpr long double operator"" _m(long double m){
        assert(m >= 0.0l && m < 10.0l); // c++14
        return m;
    }
    constexpr long double operator"" _s(long double s){
        return s;
    }
    
    
    // c++14
    constexpr long double operator""m(long double m){
        return m;
    }
    constexpr long double operator""s(long double s){
        return s;
    }
    

    I have changed my compiler setting in my pro file:

    CONFIG += c++14
    

    I get this error/warning:

    C:\Users\\Documents\programming\testing\test_rules\testrules.h:18: error: invalid suffix on literal; C++11 requires a space between literal and identifier
    

    Is there a way to suppress this error when compiling c++14?
    BTW, it still seems to compile correctly. It just throws an error.

    For reference.



  • Hi, if your code isn't inside a standard library header you'll still need that _ prefix even for c++14, i.e. perhaps try:

    // c++14
    constexpr long double operator""_m(long double m){
        return m;
    }
    constexpr long double operator""_s(long double s){
        return s;
    }
    


  • @hskoglund said in c++11 error message when compiling c++14:

    Hi, if your code isn't inside a standard library header you'll still need that _ prefix even for c++14

    Why would being in a standard library have any effect on using a literal as defined in c++14? The examples from CppCoreGuidelines show this exact example of using m and s. The point is to show intent through liberal use of literals. Where does it say you have to use underscores? I didnt see that in the other link I provided. Confused...

    Maybe I was not clear: The code compiles just fine and runs just fine. It is Qt Creator complaining when there is actually no compilation error.



  • Crap, it does say this:

    may only appear as part of a standard library header
    

    That is strange. Thanks for the heads up.



  • Just as @hskoglund said all error messages go away if doing only this:

    constexpr long double operator""_m(long double m){
        assert(m >= 0.0l && m < 100.0l); // c++14
        return m;
    }
    constexpr long double operator""_s(long double s){
        return s;
    }
    

    Note no spaces between " and _. Darn reserved names and stuff!



  • @fcarney

    Are talking about the "code model" warnings/errors in qt creator?

    You can change it under "Tools"->"Options"->"{} C++"->"Code Model"
    Under "Diagnostic Configuration" you can choose another configuration or adjust the one used. The same syntax can be used as you would in suppressing warnings for compilation.


Log in to reply