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

Possible ICC>2021 C++ compiler bug

VivienC
Novice
587 Views

When compiling/running this code

 

#include <iostream>

struct Aok1{
    std::string s {"foobar"};
    int i{42};
    operator std::string (){ return s;} // return by value
};

struct Aok2{
    std::string s {"foobar"};
    int i{42};
    operator const std::string & ()const{ return s;} // const ref
};

struct Ako{
    std::string s {"foobar"};
    int i{42};
    operator std::string & (){ return s;} // non-const ref
};


int main(int argc, char *argv[])
{
    {
    std::cout << "---------------" << std::endl;
    Aok1 a;
    std::cout << "first call" << std::endl;
    const std::string b = (std::string)a;
    std::cout << b << std::endl;
    std::cout << "second call" << std::endl;
    const std::string c = (std::string)a;
    std::cout << c << std::endl;
    std::cout << "voila..." << std::endl;
    }

    {
    std::cout << "---------------" << std::endl;
    Aok2 a;
    std::cout << "first call" << std::endl;
    const std::string b = (std::string)a;
    std::cout << b << std::endl;
    std::cout << "second call" << std::endl;
    const std::string c = (std::string)a;
    std::cout << c << std::endl;
    std::cout << "voila..." << std::endl;
    }

    {
    std::cout << "---------------" << std::endl;
    Ako a;
    std::cout << "first call" << std::endl;
    const std::string b = (std::string)a;
    std::cout << b << std::endl;
    std::cout << "second call" << std::endl;
    const std::string c = (std::string)a;
    std::cout << c << std::endl;
    std::cout << "voila..." << std::endl;
    }

    return 0;
}
you will end-up with the following result
 
---------------
first call
foobar
second call
foobar
voila...
---------------
first call
foobar
second call
foobar
voila...
---------------
first call
foobar
second call

voila...

 

It seems the compiler is "moving" when a non-const reference cast operator is used here.

 

This won't happend with gcc, clang or with previous release of intel c++ compiler.

 

godbolt link : https://godbolt.org/z/xxb74qccM

0 Kudos
3 Replies
SeshaP_Intel
Moderator
564 Views

Hi

 

Thank you for posting in Intel Communities.

We have tried the latest version icc (2021.7.0) to run this program. And it is working as expected.

Please find the attached screenshot for the results.

SeshaP_Intel_0-1665144873196.png

 

Please try the latest version and let us know if you still face any issues.

 

Thanks and Regards,

Pendyala Sesha Srinivas

 

 

0 Kudos
SeshaP_Intel
Moderator
453 Views

Hi,


We haven't heard back from you. Could you please provide an update on your issue?


Thanks and Regards,

Pendyala Sesha Srinivas


0 Kudos
SeshaP_Intel
Moderator
364 Views

Hi,


We assume that your issue is resolved. If you need any additional information, please post a new question as this thread will no longer be monitored by Intel.


Thanks and Regards,

Pendyala Sesha Srinivas


0 Kudos
Reply