How can I set a breakpoint in template function

L

learning_C++

Hi,
Is it possible for me to set a breakpoint in template function in C++?
I have a program like this:

#include<iostream>
#include<vector>

using namespace std;

template <class T> T vector_sum(vector<T> &v){
vector<T>::iterator iter;
T result;
for(iter=v.begin();iter<v.end();iter++)
result=result+*iter;
return result;
}

int main(){
vector<int> v1;
vector<double> v2;
int sum1;
double sum2;

v1.push_back(1);
v1.push_back(2);
sum1=vector_sum(v1);
cout<<"The result(int) is: "<<sum1<<endl;

v2.push_back(1.1);
v2.push_back(2.2);
sum2=vector_sum(v2);
cout<<"The result(double) is: "<<sum2<<endl;
return 0;
}

I can not get the correct results, so I hope to use gdb to check. But
I can not set the breakpoint in template vector_sum().

Please help me!
 
V

Victor Bazarov

learning_C++ said:
Is it possible for me to set a breakpoint in template function in C++?

Please ask this in a newsgroup dedicated to your debugger. This is
not a C++ _language_ question.

Victor
 
A

Arne Adams

[snip]
template <class T> T vector_sum(vector<T> &v){
vector<T>::iterator iter;
T result;
for(iter=v.begin();iter<v.end();iter++)
result=result+*iter;
return result;
}
in the other thread: "How can I return a value in template function?" Sumit
Rajan already pointed out that you need to initialize your local result,
otherwise you add values to an arbitrary starting value yielding an
arbitrary result.

Sumit Rajan said:
T result = T();
You need to initialize result to 0.
The form Sumit used works for built in types as well as for default
constructible class types.
For built in types this form is called zero initialization and does exactly
that (with an appropriate zero).
(However, for class types you would not need to explicitly default
initialize the local variable).

Not every debugger can handle breakpoints in templates equally gracefully -
if you cannot change the debugger you might need to resort to old
printf-style debugging :-(

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top