Created
June 17, 2024 22:02
-
-
Save mmomtchev/169dbe4bdfdab443db9bf5d2906dc09c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()); | |
} |
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
https://stackoverflow.com/questions/10644910/does-stdruntime-error-copy-the-string-passed-in-the-constructor?__cf_chl_tk=XholeIXP.sUy1O3uf5mRdb1KPDOmKeAC92zxfSAPVZQ-1718656505-0.0.1.1-5332