Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I add a breakpoint only for a specific template instantiation?

Let's say I have a templated class:

template <typename T>
class A
{
public:
   void foo() 
   {
      int i = 0; //breakpoint here only for type A<int>
   }
}

Can i somehow add a breakpoint, in Visual studio, that will only break inside foo for a certain instantiation ?

Like only for A<int>::foo ?

Let's assume I have 100's of templated A instantiations with different types.

Edit:

I do know how to make instantiations in a way that i could specialize a certain type.
The question is can i do it without specialization?

like image 608
Yochai Timmer Avatar asked Jan 01 '15 09:01

Yochai Timmer


People also ask

How do I add a conditional breakpoint in Visual Studio?

Right-click the breakpoint symbol and select Conditions (or press Alt + F9, C). Or hover over the breakpoint symbol, select the Settings icon, and then select Conditions in the Breakpoint Settings window.

How do I force a template instantiation?

To instantiate a template function explicitly, follow the template keyword by a declaration (not definition) for the function, with the function identifier followed by the template arguments. template float twice<float>(float original); Template arguments may be omitted when the compiler can infer them.

Is it necessary to instantiate a template?

In order for any code to appear, a template must be instantiated: the template arguments must be provided so that the compiler can generate an actual class (or function, from a function template).


1 Answers

I found it.
Just put a breakpoint in the line you want (I'll show an example with std::shared_ptr<>).
Then go to the Breakpoints window and notice that when it breaks, there's a little + next to the breakpoint that will open all the different instantiations.
The line in bold is the breakpoint that is currently active.

Breakpoint on a templated function

Now, unfortunately, the Breakpoints window doesn't show you the actual template instantiation.
But, you can use the call stack to see which instantiation is currently used. Or, you can right click on each of the breakpoints, and choose "Go To Disassembly".

This may give you a hint as to the actual template instantiation. Then you can choose which breakpoints and for which type you want to keep active.

Disassembly

Edit: You could also add the Function column to the Breakpoints window and see the actual template function.

enter image description here

like image 70
Yochai Timmer Avatar answered Oct 10 '22 12:10

Yochai Timmer