How to force a compiler error when no value is explicitly returned from a function

One of the most common mistakes I make is that I forget to return the result from the method / function and the compiler doesn't complain.

How can I get GCC to raise a compilation error if no result is returned? (these are usually trivial situations where there is no return in a method)

+3


source to share


3 answers


Use -Wreturn-type

(I would recommend at least -Wall

maybe also -Wextra

). It gives a warning when you don't return a value; you can use -Werror

to make it an error.



+5


source


Compile flags -Wall -Werror

. This will result in an error:

error: no return in function returning non-void



-Wall

by itself outputs a warning.

I recommend compiling with -Wextra

too.

+4


source


g++ -Werror main.cpp -o main

Warnings as errors! and -Wall helps too

+1


source







All Articles