By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,454 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

Reg. Function Templates

P: n/a
VSP
Hi,

In the following code, we have created a template function,
We expected the output to display "A::A()" and "B::B()" but we are getting
both as "B::B()".

class A
{
public:
A()
{
cout<<endl<<"A::A()";
}
};

class B
{
public:
B()
{
cout<<endl<<"B::B()";
}
};

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

int main(int argc, char* argv[])
{
func<A>();
func<B>();
return 0;
}

Please clarify.
Compiler Used: VC++ 6.0

Regards,
VSP
Aug 4 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"VSP" <ab*@abc.comwrote in message
news:f9**********@daniel-new.mch.sbs.de...
Hi,

In the following code, we have created a template function,
We expected the output to display "A::A()" and "B::B()" but we are getting
both as "B::B()".

class A
{
public:
A()
{
cout<<endl<<"A::A()";
}
};

class B
{
public:
B()
{
cout<<endl<<"B::B()";
}
};

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

int main(int argc, char* argv[])
{
func<A>();
func<B>();
return 0;
}

Please clarify.
Compiler Used: VC++ 6.0
In VC++ .net 2003 the output is the expected

A::A()
B::B()

Of course after I included <iostreamand changed cout and endl to std::cout
and std::endl.

Try a fresh build. If that doesn't work, it's a bug in VC++ 6.0. 6.0 is
fairly old, prestandard and has a few bugs. You definately need to upgrade.
Aug 4 '07 #2

P: n/a
Jim Langston wrote:
:: "VSP" <ab*@abc.comwrote in message
:: news:f9**********@daniel-new.mch.sbs.de...
::: Hi,
:::
::: In the following code, we have created a template function,
::: We expected the output to display "A::A()" and "B::B()" but we
::: are getting both as "B::B()".
:::
::: class A
::: {
::: public:
::: A()
::: {
::: cout<<endl<<"A::A()";
::: }
::: };
:::
::: class B
::: {
::: public:
::: B()
::: {
::: cout<<endl<<"B::B()";
::: }
::: };
:::
::: template<class T>
::: void func()
::: {
::: T t;
::: }
:::
::: int main(int argc, char* argv[])
::: {
::: func<A>();
::: func<B>();
::: return 0;
::: }
:::
::: Please clarify.
::: Compiler Used: VC++ 6.0
::
:: In VC++ .net 2003 the output is the expected
::
:: A::A()
:: B::B()
::
:: Of course after I included <iostreamand changed cout and endl to
:: std::cout and std::endl.
::
:: Try a fresh build. If that doesn't work, it's a bug in VC++ 6.0.
:: 6.0 is fairly old, prestandard and has a few bugs. You definately
:: need to upgrade.

Yes, this is a known bug in VC6. The compiler uses function
parameters, but not template parameters, in the function signature.
This fools the linker into believing that there is only one func().
If you change the function to

template<class T>
void func(const T* = 0)
{
T t;
}

it will work as it should.

If you try to learn C++, the free Visual C++ Express (VC8) is a much
better choice.
Bo Persson
Aug 4 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.