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

Specifying C++17 stops C++14 features



  • Environment

    Qt 5.11.3
    macOS 10.13.6 High Sierra
    Xcode version 9.4.1

    Clang++ is:

    $ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -v
    Apple LLVM version 9.1.0 (clang-902.0.39.2)
    

    Code

    C++ code is:

    template<class T, class O = T>
    using IteratorOnly = std::enable_if_t<
        !std::is_same_v<typename std::iterator_traits<T>::value_type, void>, O
    >;
    

    C++14 added type trait *_t versions and C++17 added *_v versions. So, compiling the code with C++17 is expected to work.

    Observation

    Putting CONFIG += c++14 inside QMake project file, throws one error:

    error: no template named 'is_same_v' in namespace 'std'; did you mean 'is_same'?

    Putting CONFIG += c++17 inside QMake project file, throws two errors:

    error: no template named 'enable_if_t' in namespace 'std'; did you mean 'enable_if'?

    error: no template named 'is_same_v' in namespace 'std'; did you mean 'is_same'?

    Problem

    I cannot figure out the observation cause. Is it due to a bad installation of the standard library/compiler? Any other possibility?

    Note

    I'm able to run the same code on these without any problem:

    • Windows 10, Qt 5.12.9, MS Visual Studio 2017
    • Linux openSUSE Leap 15.1, Qt 5.12.7, GCC 7.5.0


  • Finally, I used C++14 on QMake project file:

    CONFIG += c++14
    QMAKE_CXXFLAGS += -std=c++14
    

    Then this error is received:

    error: no template named 'is_same_v' in namespace 'std'; did you mean 'is_same'?

    I bypass the error by this:

    #ifdef __APPLE__
    
    // On our current macOS build environment:
    // Qt 5.11.3
    // macOS 10.13.6 High Sierra
    // Xcode version 9.4.1
    //
    // C++14 works for 'std::enable_if_t' but not `std::is_same_v`
    // C++17 works for none of them!
    //
    // A workarond is to self-define `is_same_v` according to this:
    // https://en.cppreference.com/w/cpp/types/is_same
    
    template< class T, class U >
    inline constexpr bool is_same_v = std::is_same<T, U>::value;
    
    template<class T, class O = T>
    using IteratorOnly = std::enable_if_t<
        !is_same_v<typename std::iterator_traits<T>::value_type, void>, O
    >;
    
    #else // __APPLE__
    
    template<class T, class O = T>
    using IteratorOnly = std::enable_if_t<
        !std::is_same_v<typename std::iterator_traits<T>::value_type, void>, O
    >;
    
    #endif // __APPLE__
    


  • Another observation

    CONFIG += c++14 inside QMake project file, invokes clang++ with std=gnu++1y flag:

    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -c -pipe -stdlib=libc++ -g -std=gnu++1y  -arch x86_64 -isysroot ...
    

    CONFIG += c++17 inside QMake project file, invokes clang++ with std=gnu++11 flag:

    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -c -pipe -stdlib=libc++ -g -std=gnu++11  -arch x86_64 -isysroot ...  
    


  • Yet another observation

    Putting QMAKE_CXXFLAGS += -std=c++14 inside QMake project file, passes both -std=c++14 and -std=gnu++11 to clang++:

    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -c -pipe -stdlib=libc++ -std=c++14 -g -std=gnu++11  -arch x86_64 -isysroot ...
    

    Putting QMAKE_CXXFLAGS += -std=c++17 inside QMake project file, passes both -std=c++17 and -std=gnu++11 flags to clang++:

    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -c -pipe -stdlib=libc++ -std=c++17 -g -std=gnu++11  -arch x86_64 -isysroot ...
    

  • Lifetime Qt Champion

    Upgrade your Qt version to a version which adds the proper flags or add them correctly by your own. 5.11 is too old to support the correct -std=c++17 flag.



  • @Christian-Ehrlicher Is there any reference on how to add my own proper flags to Qt 5.11? I have never done it before, thanks!


  • Lifetime Qt Champion

    Maybe in the qmake documentation. I don't use qmake since ages so I can't really help here.
    Maybe you can remove flags with QMAKE_CXXFLAGS -= -std=c++11 ?



  • Finally, I used C++14 on QMake project file:

    CONFIG += c++14
    QMAKE_CXXFLAGS += -std=c++14
    

    Then this error is received:

    error: no template named 'is_same_v' in namespace 'std'; did you mean 'is_same'?

    I bypass the error by this:

    #ifdef __APPLE__
    
    // On our current macOS build environment:
    // Qt 5.11.3
    // macOS 10.13.6 High Sierra
    // Xcode version 9.4.1
    //
    // C++14 works for 'std::enable_if_t' but not `std::is_same_v`
    // C++17 works for none of them!
    //
    // A workarond is to self-define `is_same_v` according to this:
    // https://en.cppreference.com/w/cpp/types/is_same
    
    template< class T, class U >
    inline constexpr bool is_same_v = std::is_same<T, U>::value;
    
    template<class T, class O = T>
    using IteratorOnly = std::enable_if_t<
        !is_same_v<typename std::iterator_traits<T>::value_type, void>, O
    >;
    
    #else // __APPLE__
    
    template<class T, class O = T>
    using IteratorOnly = std::enable_if_t<
        !std::is_same_v<typename std::iterator_traits<T>::value_type, void>, O
    >;
    
    #endif // __APPLE__
    

Log in to reply