Improving the debugging experience for std::function

fWe received a Visual Studio User Voice suggestion to make “StepInto” go directly to user code, skipping past standard library (std::function) implementation details. We recently worked on this suggestion and implemented it in the last version of Visual C++.

The issue:

Single-stepping through a call to an instance of std::function was a particular pain point. When debugging, you are not usually interested in the internal implementation details of the standard library. Instead, you’d like the debugger to view this as “system code” that may be skipped, taking you directly to the code you wrote.

It took no less than *22* presses of F11 to single step through the STL to actually get to user code, and 9 more to get back out after the user’s function completes in the code below:

#include "stdafx.h"
#include <functional> 
#include <iostream>

void display() {
	std::cout << "Hello" << std::endl; // step into should go here 
}

int main() {
	std::function<void()> f = &display;
	f(); // Step into here should go directly to "display()" 
}

The solution:

To fix this behavior, we used a debugger feature to control single-stepping behavior by annotating the STL code with hints about the calls to user code. So a single keypress steps from an expression that calls a std::function object (and for calls to std::invoke as well) directly to the user’s function, without stopping in any of the intervening STL machinery. Likewise, a single keypress at the end of the user’s function steps directly back out to the invocation expression without stopping in intervening STL machinery. This vastly improves users’ debugging experience.

std_Fct_stepinto2

In Visual Studio version 15.5 Preview 4 we’ve annotated our v141 toolset standard library code with hints to the debugger that enable this behavior. We know that this feature could be useful for other libraries as well and are working on a way to generalize this feature and make it available to developers.

Note: If you want the old behavior back you can choose it at compile time by defining _DEBUG_FUNCTIONAL_MACHINERY to a non-zero value.

As we often mention in our blog posts, your feedback is important to improve the Visual Studio experience, we spend a lot of time reviewing your suggestions and incorporating them into our planning for future releases.

We look forward to your feedback!

— Visual C++ Team