Templates

G

g18c

Hi, i am trying to overload a function with a number of specific
template functions without much sucess.

template<typename T> void TmplFunc(T& t)
{
}

template<typename T> void TmplFunc(list<T>& t)
{
list<T>::const_iterator fi = t.begin();
}

void main()
{
list<int> myList;
TmplFunc(myList);
}

On compilation i get an error saying none of the two overloads have a
best conversion. I appreciate for "void TmplFunc(T& t)" T could also be
a list... is there any way i can have the "void TmplFunc(list<T>& t)"
get called without resorting to calling the specific template with
"TmplFunc<list<int> >(myList)"? Or even better, i would like a template
function that can deal with any container object which has iterators!

Thanks in advance,

Chris
 
N

Neelesh Bodas

Hi, i am trying to overload a function with a number of specific
template functions without much sucess.

template<typename T> void TmplFunc(T& t)
{
}

template<typename T> void TmplFunc(list<T>& t)
{
list<T>::const_iterator fi = t.begin();
}

typename list said:
void main()

main must return int.
{
list<int> myList;
TmplFunc(myList);
}


Interestingly enough, my previous post was also for the same type of
question!!
 
B

Bob Hairgrove

Hi, i am trying to overload a function with a number of specific
template functions without much sucess.

template<typename T> void TmplFunc(T& t)
{
}

template<typename T> void TmplFunc(list<T>& t)
{
list<T>::const_iterator fi = t.begin();
}

void main()

This should be:
int main()
{
list<int> myList;
TmplFunc(myList);
}

On compilation i get an error saying none of the two overloads have a
best conversion. I appreciate for "void TmplFunc(T& t)" T could also be
a list... is there any way i can have the "void TmplFunc(list<T>& t)"
get called without resorting to calling the specific template with
"TmplFunc<list<int> >(myList)"? Or even better, i would like a template
function that can deal with any container object which has iterators!

I don't know, this works for me:

// test_template.cpp
#include <iostream>
#include <ostream>
#include <list>

template<typename T>
void TmplFunc(T& t)
{
}

template<typename T>
void TmplFunc(std::list<T>& t)
{
typename std::list<T>::const_iterator fi = t.begin();
std::cout << "Called TmplFunc(list<T>&)" << std::endl;
}

int main()
{
std::list<int> myList;
TmplFunc(myList);
}
 
C

Chris Morley

Not sure where my other post has gone so sorry if this appears twice... What
compiler is that with bob?
 
B

Bob Hairgrove

Not sure where my other post has gone so sorry if this appears twice... What
compiler is that with bob?

I tried it with MSVC++ 7.1 and Borland 5.5.1, but it would probably
work with any modern compiler.
 
C

Chris Morley

I tried it with MSVC++ 7.1 and Borland 5.5.1, but it would probably
work with any modern compiler.

MSVC++7.0 doesn't seem to be modern because it doesn't work for me!! Surely
ppl have had these problems before? I cant use 7.1 because other people in
the team use MSVC++7.0 too. This is extremely frustrating!
 
B

Ben Pope

Chris said:
MSVC++7.0 doesn't seem to be modern because it doesn't work for me!!

No, it wouldn't be classed as modern with regards templates.

Ben Pope
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top