views:

60

answers:

3

Is there a way, in VC++ (VSTS 2008), to froce a compiler error for functions that do not explicitly return a value on the default return path (Or any other quick way to locate them)?

On the same issue, is there any gaurentee as to what such functions actually return?

+1  A: 

If you enable max warning level, and treat warnings as errors, you'll surely find what you're looking for. A guess as to what will be returned otherwise: A default-constructed object of the function's return type.

Marcin
Which is to say, when dealing with built-ins, a pseudo-random thingie...
Matthieu M.
+2  A: 

I don't know exactly the warning number, but you can use #pragma warning for enforcing a specific warning to be treated as error:

Example:

#pragma warning( error: 4001)

will treat warning 4001 as error

Cătălin Pitiș
A: 

VC will warn about many instances of this problem, but fails to detect some. I've repeatedly caught it missing this problem in function templates, but I've seen int in some plain functions, too. Treating warnings as errors (compiler switch for all warnings or pragma for specifc ones) will make it impossible to overlook those it finds.

For those VC overlooks you have to use more thorough tools. AFAIK in VSTS you can also throw an /analyze switch for the compiler and have it find even more problems.

There's also many versions of lint-like programs.

Using some other compiler helps, too. Porting a VS project to GCC for the first time can be quite hard, but I think Intel's compiler can be used as a drop-in replacement for VC and compile VC projects right away. Comeau C++, too, has switches for being quite VC-compatible and has incredibly good errors messages.

sbi