c++ -std= strangeness

Michael Dickens michaelld at macports.org
Tue Feb 5 02:59:37 UTC 2019


Yet another interesting oddity came up in my debugging today trying to fix CMake to build correctly on OSX 10.5 PPC(32). The issue is in the use of std::lround, which is defined for c++11 and newer only, and, was possibly not included as part of c++11 in GCC[4-6] ... it is apparently included in GCC7 and newer.

This got me going down a rabbit hole with regard to __cplusplus (set via the -std flag) and building code that calls std::lround, so I created a simple test file as follows:
{{{
#if __cplusplus == 201707L
#warning "Using STD c++2a (will be c++20)"
#elif __cplusplus == 201703L
#warning "Using STD c++17 or c++1z (will be c++17)"
#elif __cplusplus == 201402L
#warning "Using STD c++14"
#elif __cplusplus == 201103L
#warning "Using STD c++11"
#elif __cplusplus == 199711L
#warning "Using STD c++98 or c++03"
#endif

#include <cmath>
#include <iostream>

int
main
(void)
{
  std::cout << "lround(+2.3) = " << std::lround(2.3) << std::endl;
  return 0;
}
}}}

Then, I compile this code using various -std=c++XY or gnu++XY ... doesn't really matter for this example. I also use the following compilers:

* clang++ :Apple LLVM version 10.0.0 (clang-1000.11.45.5)
* clang++-mp-7.0 : clang version 7.0.1 (tags/RELEASE_701/final)
* g++-mp-8 : gcc version 8.2.0 (MacPorts gcc8 8.2.0_3)
* g++-mp-9 : gcc version 9.0.1 20190127 (experimental) (MacPorts gcc9 9-20190127_0)

The interesting strangeness is that the clang++ compilers (both Apple and MP) do not generate errors when using -std=c++98 or -std=c++03 ... which is wrong! The #warning printout indicates that the correct __cplusplus is selected, but the build should not succeed! All g++ compilers correctly error out with these -std settings.

This lack of compliance at least by default is troubling! Maybe I'm missing something here? Are there some other flags I should be setting to keep clang from figuring out the c++ standard and using it instead of what I specify -- if this is the issue?

Thanks for any feedback! - MLD


More information about the macports-dev mailing list