Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7944 Discussions

Help with oneAPI 2021.1 C++ Compiler issue on macOS

yaf
New Contributor I
1,266 Views

I installed the initial release of Intel® oneAPI 2021.1.

I have some difficulties regarding codes that were working correctly on previous intel compilers and are working with no issue on other GNU, CLANG, ... C++ compilers

For example,

#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;
}

this sample is working correctly on a wide range of compilers including other Intel compilers but with oneAPI 2021.1 C++ Compiler on macOS is failing with:

test.cpp(7): error: expression must be an lvalue
  	std::string s = "matrix (of size N=" + SNUM(n) + ")";
  	                                       ^

compilation aborted for snum.cpp (code 2)

Host info:

>> sw_vers   
ProductName:	macOS
ProductVersion:	11.1
BuildVersion:	20C69

Compiler info:

>> icpc --version
icpc (ICC) 2021.1 Beta 20201112
Copyright (C) 1985-2020 Intel Corporation.  All rights reserved.

 

Can you advise me if there is any compatibility issue on macOS?

 

0 Kudos
1 Solution
yaf
New Contributor I
1,184 Views

@Viet_H_Intel thanks for checking this issue. I checked it again and figured out the compiler error is correct and this piece of code is wrong. Apparently, the older compilers did not capture it.

The macro should change to,

#define SNUM(x) static_cast<const std::ostringstream &>(std::ostringstream() << std::dec << x).str()

 and it is working as supposed.

View solution in original post

0 Kudos
7 Replies
AbhishekD_Intel
Moderator
1,244 Views

Hi,

 

Thanks for reaching out to us.

We tried the same sample provided by you on our environment with the same ICPC compiler, but we cannot see the errors.

So we are forwarding this issue to the SME.

 

Also as this thread is more related to the Intel C++ compiler we are moving this thread to the Intel C++ forum.

 

Warm Regards,

Abhishek

 

0 Kudos
yaf
New Contributor I
1,222 Views

Your answer is irrelevant to the question!

0 Kudos
Viet_H_Intel
Moderator
1,211 Views

It seems like a configuration issue. Clang++ (12.0) gives an error with -std=c++11

$ clang++ t.cpp -std=c++11

t.cpp:7:48: 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) + ")";

                        ^~~~~~~

t.cpp:4:17: note: expanded from macro 'SNUM'

#define SNUM(x) static_cast<std::ostringstream &>(std::ostringstream() << std::dec << x).str()

        ^                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1 error generated

$ clang++ t.cpp -std=c++98

$ /opt/intel/oneapi/compiler/2021.1.1/mac/bin/intel64/icpc t.cpp -std=c++11

t.cpp(7): error: expression must be an lvalue

     std::string s = "matrix (of size N=" + SNUM(n) + ")";

                         ^


compilation aborted for t.cpp (code 2)

$ /opt/intel/oneapi/compiler/2021.1.1/mac/bin/intel64/icpc t.cpp -std=c++98

$



0 Kudos
yaf
New Contributor I
1,207 Views

Thanks, @Viet_H_Intel.

It is very interesting, I did not notice this,

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

 

>> g++ test.cpp
>> ./a.out
matrix (of size N=100)

 

but as you mentioned with c++11

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

 

0 Kudos
yaf
New Contributor I
1,191 Views

@Viet_H_Intel I tried `g++ -std=c++98 test.cpp` and it is working correctly.

 

>> g++ -std=c++98 test.cpp
>> ./a.out 
matrix (of size N=100)
0 Kudos
yaf
New Contributor I
1,185 Views

@Viet_H_Intel thanks for checking this issue. I checked it again and figured out the compiler error is correct and this piece of code is wrong. Apparently, the older compilers did not capture it.

The macro should change to,

#define SNUM(x) static_cast<const std::ostringstream &>(std::ostringstream() << std::dec << x).str()

 and it is working as supposed.

0 Kudos
Viet_H_Intel
Moderator
1,176 Views

Thanks,

I am going to close this issue.


0 Kudos
Reply