Templates

N

Naveen

I am trying to build a very simple template function like this:

class A
{
public:

};


void fun(int k)
{
}

void fun(double k)
{
}

void fun(const A& a)
{
}

template <class T>
void myFun()
{
T t;
fun(t);
}

int main()
{
myFun<A>();
myFun<double>();
myFun<int>();
return 0;

}

But when I debug I find that each of the function calls from inside the
template function myFun() goes void fun(int k) irrespective of the template
type. Am I missing something very basic here ? I am using VC6 compiler.
 
K

Kai-Uwe Bux

Naveen said:
I am trying to build a very simple template function like this:

class A
{
public:

};


void fun(int k)
{
}

void fun(double k)
{
}

void fun(const A& a)
{
}

template <class T>
void myFun()
{
T t;
fun(t);
}

int main()
{
myFun<A>();
myFun<double>();
myFun<int>();
return 0;

}

But when I debug I find that each of the function calls from inside the
template function myFun() goes void fun(int k) irrespective of the
template type. Am I missing something very basic here ? I am using VC6
compiler.

Since we cannot see what the debugger is showing you, may I ask what is the
output of the following instrumented program is:

#include <iostream>
#include <ostream>

class A
{
public:

};


void fun(int k)
{
std::cout << "int\n";
}

void fun(double k)
{
std::cout << "double\n";
}

void fun(const A& a)
{
std::cout << "A\n";
}

template <class T>
void myFun()
{
T t;
fun(t);
}

int main()
{
myFun<A>();
myFun<double>();
myFun<int>();
return 0;

}


Best

Kai-Uwe Bux
 
N

Naveen

Kai-Uwe Bux said:
Since we cannot see what the debugger is showing you, may I ask what is
the
output of the following instrumented program is:

#include <iostream>
#include <ostream>

class A
{
public:

};


void fun(int k)
{
std::cout << "int\n";
}

void fun(double k)
{
std::cout << "double\n";
}

void fun(const A& a)
{
std::cout << "A\n";
}

template <class T>
void myFun()
{
T t;
fun(t);
}

int main()
{
myFun<A>();
myFun<double>();
myFun<int>();
return 0;

}


Best

Kai-Uwe Bux

I just tried it and the output is:
int
int
int

Its crazy..
 
K

Kai-Uwe Bux

Naveen said:
I just tried it and the output is:
int
int
int

Its crazy..

True.


On my machine, the output is:

A
double
int

I am pretty certain that in this regard, my compiler is correct and your
compiler is wrong. Maybe, you should file a bug report or upgrade to a
newer version.


Best

Kai-Uwe Bux
 
S

Salt_Peter

I am trying to build a very simple template function like this:

class A
{
public:

};

void fun(int k)
{

}

void fun(double k)
{

}

void fun(const A& a)
{

}

template <class T>
void myFun()
{
T t;
fun(t);

}

int main()
{
myFun<A>();
myFun<double>();
myFun<int>();
return 0;

}

But when I debug I find that each of the function calls from inside the
template function myFun() goes void fun(int k) irrespective of the template
type. Am I missing something very basic here ? I am using VC6 compiler.

Upgrade the compiler, VC6 is blind when it comes to templates. Newer
versions are mostly free. You should get a result as follows

#include <iostream>

class A { };

void fun(int k)
{
std::cout << "fun(int)\n";
}

void fun(double k)
{
std::cout << "fun(double)\n";
}

void fun(const A& a)
{
std::cout << "fun(const A&)\n";
}

template <class T>
void myFun()
{
T t;
fun(t);
}

int main()
{
myFun<A>();
myFun<double>();
myFun<int>();
}

/*
fun(const A&)
fun(double)
fun(int)
*/

the following works too:

template <class T>
void myFun(const T& t)
{
fun(t);
}

int main()
{
myFun(A());
myFun(1.1);
myFun(99);
}
 
N

Naveen

Kai-Uwe Bux said:
True.


On my machine, the output is:

A
double
int

I am pretty certain that in this regard, my compiler is correct and your
compiler is wrong. Maybe, you should file a bug report or upgrade to a
newer version.


Best

Kai-Uwe Bux

I thought I was missing something very basic.. thanks for your reply.
BTW, when I tried the following piece of code (passing a template argument
to the function) I got the output as
A
double
int


#include <iostream>
#include <ostream>

class A
{
public:

};


void fun(int k)
{
std::cout << "int\n";
}

void fun(double k)
{
std::cout << "double\n";
}

void fun(const A& a)
{
std::cout << "A\n";
}

template <class T>
void myFun(const T& t1)
{
T t;
fun(t);
}

int main()
{
myFun<A>(A());
myFun<double>(double());
myFun<int>(int());
return 0;

}
 

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,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top