Allan McLemore
11/26/2008 9:06:00 AM
"Kai-Uwe Bux" <jkherciueh@gmx.net> wrote in message
news:492d100e$0$17070$6e1ede2f@read.cnntp.org...
> Naveen wrote:
>
>>
>> "Kai-Uwe Bux" <jkherciueh@gmx.net> wrote in message
>> news:492d0ded$0$17067$6e1ede2f@read.cnntp.org...
>>> Naveen wrote:
>>>
>>>> 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
>>
>> 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
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;
}