Declaring a function noexcept conditionally on the testing flag?

  assert, c++, noexcept, testing, throw

If one is using exceptions to replace assertions for debugging/testing purposes.

Does it make sense to declare a function noexcept conditionally on the testing flag or this would mess too much with the function interface?

#ifdef MYTESTING
#define MYASSERT(x) if(not x) throw std::logic_error("error not "#x)
#else
#define MYASSERT(x) assert(x);
#endif

double f(double x) 
#if not defined(MYTESTING)
noexcept
#endif
{
   MYASSERT(x >= 0);
   return legacy_c_function(x);
}

(I know that legacy_c_function is a C function and it is not going to throw an exception).

I know that many (standard) function are not declared noexcept just in case one wants to test the function internally.

In principle this could have the best of both worlds?

Although unlikely (?), I can see that this method of testing might have diminished value because it can have observable effects in other parts of the code that depend on the noexceptness of this function.

Am I missing something?

Source: Windows Questions C++

LEAVE A COMMENT