0

Suppose I have a custom static assert implementation (because I need to target a compiler that doesn't have static_assert built in). I want to craft a test that checks that

MY_STATIC_ASSERT(false);

indeed asserts. If I just write such code - it will not compile (and so not run). I'd rather have some piece of code that compiles fine when the code above fails and fails to compile when the code above does compile.

Is that possible? Can I have a compile-time (or at least a runtime) check that my static assert indeed asserts for "false"?

sharptooth
  • 167,383
  • 100
  • 513
  • 979

1 Answers1

1

Sure, you can have a "compile-time" check - as long as you're compiling something else entirely:

// test_my_static_assert.cpp

#include "my_static_assert.h"

int main() {
    MY_STATIC_ASSERT(false);
}

// compile.sh
if g++ test_my_static_assert.cpp; then
    echo "MY_STATIC_ASSERT failed! Compile succeeded!"
fi

Or something. But it'd have to be in a separate program entirely.

Barry
  • 286,269
  • 29
  • 621
  • 977