19

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?

3 Answers 3

28

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

1
  • 5
    In the breakpoints window, you can see actual template instantiations by showing the Function column. In breakpoints window, select Function under the Columns drop down menu. Once the Function column is visible, you can even filter by the template type name :)
    – John
    Apr 21, 2016 at 6:23
5

You can add some code that will only be executed if T is an int, and set a breakpoint there. You could use std::is_same for that, or if you don't have the required header included and don't wish to add that, you can write a trivial function of your own:

template <typename T>
bool isint() { return false; }

template <>
bool isint<int>() { return true; }

template <typename T>
void f() {
    if (isint<T>())
        isint<T>();
    //  ^^^^^^^^^^^ set a breakpoint on that line

    // rest of your function
}

int main()
{
    f<char>();
    f<short>();
    f<int>();
    f<long>();
}

Testing shows that the breakpoint is hit only after f<char> and f<short> have already been called.

4

Since it's for debugging purposes, write a specialization covering that instantiation explicitly, putting the breakpoint there:

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

EDIT: Nested template + specialization

Since rewriting your class is not feasible, let's implement the function to be debugged inside a nested template, and specialize it:

template<typename T>
class A
{
public:
    void foo()
    {
        foo_impl<T,void>::exec();
    }

private:
    template<typename T, typename DUMMY>
    struct foo_impl
    {
        static void exec()
        {
            //Default impl
        }
    };

    template<typename DUMMY>
    struct foo_impl<int,DUMMY>
    {
        static void exec()
        {
            int i = 0; //Breakpoint here
        }
    }
};
3
  • It's not a a simple class where i can re-write it with a sepcialization. And I can't inherit from it because there's 1000's of lines where it's used. But a valid idea nonetheless Jan 1, 2015 at 9:53
  • Ah, if these 1000 uses could accommodate a typedef of the specialization rather than the specialization itself, you'd be saved.
    – didierc
    Jan 1, 2015 at 10:06
  • @didierc you can always write an ugly macro... :)
    – Manu343726
    Jan 1, 2015 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.