1

I'm working under Sun Studio 12.3 on SunOS 5.11 (Solaris 11.3). Its providing a spurious warning:

"<file.h>", line 1: Warning: "<function>" is expected to return a value.

The function is part of a base class interface and looks like so. I only mention the base class interface to avoid the "why would you do that" discussions. A non-member function like below is enough to trigger it.

int foo()
{
    throw runtime_error("Not implemented");
}

I found mention of similar problems. For example, the Xapian-core change log states:

  • Disable " is expected to return a value" warning from Sun's C++
    compiler, as it fires for functions ending in a "throw" statement. Genuine
    instances will be caught by compilers with superior warning machinery.

According to SunStudio C++ compiler pragma to disable warnings, I can use -erroff=voidretw. I'm concerned about -erroff=voidretw because it might suppress valid findings. Also, if I go with the #pragma, then I need to push and pop it to avoid cross pollinating into user code. I don't know how to push and pop warning states under Sun Studio.

My question is, how do I disable the warning for the one function in the header under Sun Studio?


This looks like a duplicate even though the message is different: How to silence 'The last statement should return a value' warning? I think I am going to vote to close this as a duplicate.

Community
  • 1
  • 1
jww
  • 97,681
  • 90
  • 411
  • 885
  • 1
    If you can modify that function, you could just add a `return 0;` after the `throw`. Of course, then other compilers will probably warn about unreachable code, so you'll need to surround it with some Sun Studio specific `#ifdef` – Praetorian Jun 09 '16 at 04:45
  • If dont need to return anything and simply raise exception why not you change the method to void foo(). – sagar Jun 09 '16 at 08:42
  • @sagar - Its an interface. Derived classes will return a value. – jww Jun 09 '16 at 08:56
  • 1
    Given the [C++ Standard](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf) states "a return statement with any other operand **shall be used** only in a function whose return type is not `cv` void", that "Genuine instances will be caught by compilers with superior warning machinery." statement comes across as parochial, arrogantly close-minded misguided whining from the Xapian developers, literally over being held to a standard. Since the code doesn't meet the C++ Standard, one could argue an actual error and failure to compile the code could be appropriate. – Andrew Henle Jun 09 '16 at 13:50

3 Answers3

2

If your compiler supports C++11 you can decorate that function with a noreturn attribute:

[[noreturn]] int foo()
{
    throw runtime_error("Not implemented");
}
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
2

Sun Studio allows to mark that kind of functions with

#pragma does_not_return(foo)
Alexander Gorshenev
  • 2,769
  • 18
  • 33
0

What about ?

int foo()
{
    return throw runtime_error("Not implemented"), 0;
}

Do you get the warning?

max66
  • 65,235
  • 10
  • 71
  • 111