[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Templates

Allan McLemore

11/26/2008 8:39:00 AM

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.


5 Answers

Kai-Uwe Bux

11/26/2008 8:51:00 AM

0

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

Allan McLemore

11/26/2008 8:56:00 AM

0


"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..


Kai-Uwe Bux

11/26/2008 9:00:00 AM

0

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

Salt_Peter

11/26/2008 9:04:00 AM

0

On Nov 26, 3:39 am, "Naveen" <a...@a.com> 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.

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);
}

Allan McLemore

11/26/2008 9:06:00 AM

0


"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;

}