Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there real life cases when C4930 Visual C++ warning doesn't indicate an error?

Visual C++ can emit C4930 "unused function prototype" warning in the following case:

void SomeUsefulFunction()
{
    SomeResourceLock lock(); //C4930 - unused function prototype
    //code requiring the above lock
}

in the above code the intention was to construct a stack-allocated RAII object:

void SomeUsefulFunction()
{
    SomeResourceLock lock; //okay
    //code requiring the above lock
}

but since the parentheses were typed variable definition turned into a function prototype and so no RAII object is constructed and there's no "lock" object and code behavior is changed.

Also the warning is only triggered when an unused prototype is inside a function, not at class level. It seems pretty useless to have a function prototype inside a function and not call the prototyped function.

Now I'm tempted to use pragma warning to make Visual C++ treat that particular warning as error.

Are there any real life cases when C4930 doesn't accompany an error?

like image 546
sharptooth Avatar asked Mar 22 '11 07:03

sharptooth


People also ask

Why is it a good idea to always enable compiler warnings in C?

A compiler warning signals a potentially serious problem in your code. The problems listed above are almost always fatal; others may or may not be, but you want compilation to fail even if it turns out to be a false alarm. Investigate each warning, find the root cause, and fix it.

What does a warning mean when compiling ac program?

Compiler warnings are messages produced by a compiler regarding program code fragments to be considered by the developer, as they may contain errors.


1 Answers

C++ makes it legal to make function-local declarations, although I've rarely seen this feature used. It's often an error, but for instance, if you really used such a declaration and called the matching function:

int main()
{
    int foo();
    return foo();
}

And then later removed the function call:

int main()
{
    int foo();
    return 0;
}

It would be stupid if suddenly it didn't compile anymore.

So, real-world cases where this does not indicate an error on your end are next to nonexistant, but compilers have to account for imaginary-world issues too, because sometimes the real world gets pretty unreal. Showing a warning seems like a good tradeoff for me: the compiler tells you it's unsure if it's what you really wanted.

Point is, compiler writers can't make it an error, because it's syntactically valid. If you want to treat it as an error in your code, it probably won't cause you any problem.

like image 188
zneak Avatar answered Nov 15 '22 14:11

zneak