Skip to content

Instantly share code, notes, and snippets.

@mmomtchev
Created June 17, 2024 22:02
Show Gist options
  • Save mmomtchev/169dbe4bdfdab443db9bf5d2906dc09c to your computer and use it in GitHub Desktop.
Save mmomtchev/169dbe4bdfdab443db9bf5d2906dc09c to your computer and use it in GitHub Desktop.
// Does std::logic_error copy the string
// or takes a reference?
//
// The C++ specifications are deliberately vague
//
// With MSVC, try compiling this with 1 and with 0
// You will be very surprised
//
#define _HAS_EXCEPTIONS 1
#include <stdexcept>
#include <string>
int main() {
std::string msg{"Message"};
printf("msg %p\n", msg.c_str());
std::logic_error err{msg};
printf("err %p\n", err.what());
}
@mmomtchev
Copy link
Author

MSVC carries two different std::exception implementations. The macro _HAS_EXCEPTIONS selects the one to be used. This is a horrible pitfall when linking code compiled with and without it, since sizeof(std::exception) won't be the same.

The full one:

%VCToolsInstallDir%\include\vcruntime_exception.h

The light one:

%VCToolsInstallDir%\include\exception

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment