Skip to content

Instantly share code, notes, and snippets.

@KayEss
Last active August 29, 2015 05:42
Show Gist options
  • Save KayEss/2a7bd77f718e3c895749 to your computer and use it in GitHub Desktop.
Save KayEss/2a7bd77f718e3c895749 to your computer and use it in GitHub Desktop.
`DEBUG` and release builds -- is it reasonable to expect the compiler to completely optimise away the `check` function on non-`DEBUG` builds, but leave the side effects of the arguments alone?
#ifdef DEBUG
inline void check(const auto &b) {
if ( !b ) std::terminate();
}
#else
inline void check(const auto &) {
}
#endif
// Writes the data to the file. Returns true on success
bool save_file(std::string filename, std::string data);
int main() {
// This completely goes in release builds
check("Optimised out?");
// save_file is called, but nothing else happens in release builds
check(save_file("test.txt", "Side effect left in?\n"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment