- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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;
}
---------------
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
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Please try the latest version and let us know if you still face any issues.
Thanks and Regards,
Pendyala Sesha Srinivas
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
We haven't heard back from you. Could you please provide an update on your issue?
Thanks and Regards,
Pendyala Sesha Srinivas
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page