Apple clang version 12.0.0 (clang-1200.0.32.28) issue

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.,
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


Answered by yafa in 658310022
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()

Just because you were doing something a certain way in the past doesn't mean it was ever correct. As the error message says, you are using a reference to a non-const temporary object, which is not allowed.

You could change it from a static_cast to a constructor and C++11 would be happy. But that would break it for older versions.

Ideally, just do the output operator normally in the first place.

Code Block
std::cout << "matrix (of size N=" << n << ")" << std::endl;

and that works with any version.
@Etresoft, thanks for your reply. The example was just for demonstration, and It is not just this line that is failing.
It is not wise that I change lines of codes in other people's libraries.
If I understand you correctly, you say that this line of code is wrong and the CLANG error is correct!
Accepted Answer
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()

Apple clang version 12.0.0 (clang-1200.0.32.28) issue
 
 
Q