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

Problem with operator= of [[nodiscard]] class

LPoirel
Beginner
1,018 Views

Hi,

If I write a class with [[nodiscard]] attribute, with an operator= that returns *this by reference, I get a warning #3931: ignoring return value type with "nodiscard" attribute.

 

Since the nodiscard object is returned by reference and not by value, there should be no such warning.

 

Here is a reproducible example that produces this error on icc 18.0 to icc 2021.5 in https://godbolt.org/z/jqs41Ebrd 

 

struct [[nodiscard]] S {
    S& operator=(const S&) {return *this;}  // return a S by reference
};

int main(){
    S s;

    s = S{};  // warning with intel compiler
    static_cast<void>(s = S{});  // OK, the warning is silenced
}
0 Kudos
1 Solution
VidyalathaB_Intel
Moderator
983 Views

Hi Louis,

 

Thanks for reaching out to us.

 

The issue raised by you is reproducible when trying with icc (latest 2021.5.0) from our end as well.

Could you please try compiling with icpx/icx (latest 2022.0) compiler instead?

It is working as expected without throwing any such warnings when we tried it from our end.

 

Also, Intel Classic Compiler will enter "Legacy Product Support" mode, signaling the end of regular updates. Please refer to the article below for more details.

https://www.intel.com/content/www/us/en/developer/articles/technical/adoption-of-llvm-complete-icx.html

 

 

Regards,

Vidya.

 

View solution in original post

0 Kudos
4 Replies
VidyalathaB_Intel
Moderator
984 Views

Hi Louis,

 

Thanks for reaching out to us.

 

The issue raised by you is reproducible when trying with icc (latest 2021.5.0) from our end as well.

Could you please try compiling with icpx/icx (latest 2022.0) compiler instead?

It is working as expected without throwing any such warnings when we tried it from our end.

 

Also, Intel Classic Compiler will enter "Legacy Product Support" mode, signaling the end of regular updates. Please refer to the article below for more details.

https://www.intel.com/content/www/us/en/developer/articles/technical/adoption-of-llvm-complete-icx.html

 

 

Regards,

Vidya.

 

0 Kudos
VidyalathaB_Intel
Moderator
945 Views

Hi Louis,


Reminder:

Has the information provided helped? Please let us know if you have any updates regarding the query.


Regards,

Vidya.


0 Kudos
LPoirel
Beginner
936 Views

Hi Vidya,

Indeed, everything is fine with icpx/icx  2022.0

Thanks for your answer.

 

Regards,

Louis

0 Kudos
VidyalathaB_Intel
Moderator
927 Views

Hi Louis,


Thanks for accepting our solution.

As the issue is resolved we are closing this thread. Please post a new question if you need any additional information from Intel as this thread will no longer be monitored.


Have a Nice Day!


Regards,

Vidya.


0 Kudos
Reply