5

I am working on a multiplatform project, and some platforms have disabled features, and in the interface for those features, a common thing I do is something like this:

bool Foo::bar() const {
    // disabled
    abort();
}

GCC/LLVM do not require that non-void functions return values (they just give a warning), and in this case, where I call abort(), they are smart enough to not even give a warning (since the function will never return anyway).

Is there a way (compile flag?) to make Visual C++ 2010 behave the same way, so I don't keep breaking the Windows build? I know I could always return the value after the abort, but when working on the other platforms I usually forget about that, and the behavior of not giving an error seems more appropriate.

reuben
  • 3,360
  • 23
  • 28
fbafelipe
  • 4,862
  • 2
  • 25
  • 40
  • 5
    On the flip side of this, when I *do* include the useless return, it sometimes gives me an "Unreachable Code" warning... lol – Mysticial Jul 08 '12 at 19:44
  • 1
    Calling `abort` directly seems like a bad practice. Why not use `assert(false)`? – Juraj Blaho Jul 08 '12 at 19:46
  • 1
    @JurajBlaho assert(false) will result in warnings (GCC/LLVM) on the release build. – fbafelipe Jul 08 '12 at 19:47
  • 1
    ...and depending on the environment, assert(FALSE) may turn into a no-op after the preprocessor has had its way with the sources. – reuben Jul 08 '12 at 19:51

3 Answers3

4

__declspec(noreturn) should have this effect in MSVC++ compilers. I would expect standard abort to be declared as __declspec(noreturn). But since compiler generates errors in your example, then it probably isn't declared that way. I would suggest that you check the declaration of abort. If it has no __declspec(noreturn) in it, you should add it somehow.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
2

In general, the Visual C++ compilers allow you to annotate a function with __declspec(noreturn) to indicate that the function will never return. This helps the compiler realize that any code after a call to such a function is unreachable.

The problem you're hitting here is covered in this SO question. To make a longer story short, abort() doesn't have this annotation in the Visual C++ runtime headers.

Community
  • 1
  • 1
reuben
  • 3,360
  • 23
  • 28
1

As you want to disable the error add /w34716 as compile flag (assuming you are using warning level 3 or higher) - now only a warning is issued (if the warning level is 3 or higher). You could also disable the warning (/wd4716), but that is probably no good idea - depending on the number of warnings due to this behaviour.

Stefan
  • 1,091
  • 7
  • 10