Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: How to trigger a compiler error when function return value is unused?

Tags:

Let's say I have a normalize function defined as:

Vec3f Vec3f::getNormalized() const {    return (*this)/this->length(); } 

Is it somehow possible to create a compile-time error if this function is used without something storing it's return value? ;

v.getNormalized(); // which most definitely is a typo 

..instead of..

v = v.getNormalized();  
like image 856
Viktor Sehr Avatar asked Apr 27 '11 11:04

Viktor Sehr


1 Answers

In GCC, use -Wunused-result to trigger the warning when a function's return value is ignored. And if you want an error instead of warning, use -Werror to convert all warnings into errors. For more information see GCC Warning Options.

There does not seem to be an equivalent warning for the Visual C++ compiler. (If I am wrong, please edit this reply with the Visual C++ information.)

like image 192
Ashwin Nanjappa Avatar answered Oct 01 '22 19:10

Ashwin Nanjappa