I have some difficulties regarding codes that were working correctly on previous CLANG compilers and are working with no issue on other GNU, older CLANG, ... C++ compilers
e.g.,
it is working as supposed with,
But it fails with c++11,
The compiler is,
e.g.,
Code Block cpp #include <iostream> #include <sstream> #include <string> #define SNUM(x) static_cast<std::ostringstream &>(std::ostringstream() << std::dec << x).str() int main() { int n = 100; std::string s = "matrix (of size N=" + SNUM(n) + ")"; std::cout << s << std::endl; }
it is working as supposed with,
Code Block bash >> g++ test.cpp >> ./a.out matrix (of size N=100)
But it fails with c++11,
Code Block cpp >> g++ -std=c++11 test.cpp test.cpp:7:41: error: non-const lvalue reference to type 'basic_ostringstream<...>' cannot bind to a temporary of type 'basic_ostringstream<...>' std::string s = "matrix (of size N=" + SNUM(n) + ")"; ^~~~~~~ test.cpp:4:17: note: expanded from macro 'SNUM' #define SNUM(x) static_cast<std::ostringstream &>(std::ostringstream() << std::dec << x).str() ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1 error generated.
The compiler is,
Code Block cpp >>g++ --version Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1 Apple clang version 12.0.0 (clang-1200.0.32.28) Target: x86_64-apple-darwin20.2.0 Thread model: posix InstalledDir: /Library/Developer/CommandLineTools/usr/bin
After checking the code carefully, I figured out the macro is wrong and the compiler error is correct. Apparently, other compilers do not catch this issue.
Code Block cpp #define SNUM(x) static_cast<const std::ostringstream &>(std::ostringstream() << std::dec << x).str()