Intel® C++ Compiler
Support and discussions for creating C++ code that runs on platforms based on Intel® processors.
7744 Discussions

Problem with operator= of [[nodiscard]] class

LPoirel
Beginner
509 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
474 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.h...

 

 

Regards,

Vidya.

 

View solution in original post

4 Replies
VidyalathaB_Intel
Moderator
475 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.h...

 

 

Regards,

Vidya.

 

VidyalathaB_Intel
Moderator
436 Views

Hi Louis,


Reminder:

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


Regards,

Vidya.


LPoirel
Beginner
427 Views

Hi Vidya,

Indeed, everything is fine with icpx/icx  2022.0

Thanks for your answer.

 

Regards,

Louis

VidyalathaB_Intel
Moderator
418 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.


Reply