Is it possible to compile the application code I write in a "debug" version (i.e. with symbols, ability to use breakpoints and step thru execution, watch variables, and all those goodies which aren't available in a release build) while forcing any underlying STL functionality to be optimized by the compiler (e.g. things like skipping array bounds checking and other performance enhancements) like a "release" build? I don't feel the need to step down into STL code during debugging and would rather keep the code running fast and have smaller binaries.
If so, how to configure? #defines? project settings or compiler flags? And is it "safe"?
I'm using VS2019 but the question applies to g++ and other compilers as well.
In VS, you have Step Into (F11) and Step Over (F10) which either steps into a function on debug or steps over a function if you don't want to trace through a particular function (such as a standard library function).
With libstdc++ (GCC's implementation of the standard library) the "debug mode" is turned off by default so as long as you don't pass -D_GLIBCXX_DEBUG you won't get any range checking in operator[] and iterators, and so on.
To use the libstdc++ debug mode, compile your application with the compiler flag -D_GLIBCXX_DEBUG.
GCC has an optimization flag -Og that enables some optimizations while still providing good debugging experience. I haven't actually tested to be honest but it sounds great.
Optimize debugging experience. -Og should be the optimization level of choice for the standard edit-compile-debug cycle, offering a reasonable level of optimization while maintaining fast compilation and a good debugging experience. It is a better choice than -O0 for producing debuggable code because some compiler passes that collect debug information are disabled at -O0.
As for turning on debug symbols with GCC you can use the -g flag (although there are other flags you can use). Just don't combine it with the -s flag or the strip command.
To tell GCC to emit extra information for use by a debugger, in almost all cases you need only to add -g to your other options.
libstdc++ could be compiled with a different optimization level than what you use to compile your own code files with but that will only affect the parts that gets "linked". It probably won't affect "header-only" stuff because it is compiled wherever it is included. E.g. if libstdc++ is compiled with -O2 and your code that uses std::sort is compiled with -Og then the std::sort code will end up using -Og because it's a template that gets compiled, instantiated and optimized as part of a translation unit that belongs to your code.