473,395 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

template confusion

I cannot figure out why the following program instantiates two type
"2" instances instead of one "1" and one "2".

Could someone plz enlighten me?

Many thanks

Here's the code

-------------------------------------------------------------------------------
#include <iostream>
using namespace std;

class Base
{
public:

virtual int GetType()const=0;
};

class Dev1 : public Base
{
public:

int GetType()const{return 1;}
};

class Dev2 : public Base
{
public:

int GetType()const{return 2;}
};

struct BaseHolder
{
Base* pBase;

BaseHolder(Base* pB):pBase(pB)
{}
};

template <class T>
BaseHolder CreateABaseHolder()
{
return BaseHolder(new T());
}
int main()
{
BaseHolder h1 = CreateABaseHolder<Dev1>();
BaseHolder h2 = CreateABaseHolder<Dev2>();

cout << "\nID of base in h1 (should be == 1) = " <<
h1.pBase->GetType();
cout << "\nID of base in h2 (should be == 2)= " <<
h2.pBase->GetType();

return 0;
}
-------------------------------------------------------------------------
Jan 9 '06 #1
9 1482
Floogle wrote:
I cannot figure out why the following program instantiates two type
"2" instances instead of one "1" and one "2".

Could someone plz enlighten me?

Many thanks

Here's the code

-------------------------------------------------------------------------------
#include <iostream>
using namespace std;

class Base
{
public:

virtual int GetType()const=0;
};

class Dev1 : public Base
{
public:

int GetType()const{return 1;}
};

class Dev2 : public Base
{
public:

int GetType()const{return 2;}
};

struct BaseHolder
{
Base* pBase;

BaseHolder(Base* pB):pBase(pB)
{}
};

template <class T>
BaseHolder CreateABaseHolder()
{
return BaseHolder(new T());
}
int main()
{
BaseHolder h1 = CreateABaseHolder<Dev1>();
BaseHolder h2 = CreateABaseHolder<Dev2>();

cout << "\nID of base in h1 (should be == 1) = " <<
h1.pBase->GetType();
cout << "\nID of base in h2 (should be == 2)= " <<
h2.pBase->GetType();

return 0;
}
-------------------------------------------------------------------------


I get this:
$ ./baseholder

ID of base in h1 (should be == 1) = 1
ID of base in h2 (should be == 2) = 2
On which planet did you compile this?

/S
--
Stefan Naewe
naewe.s_AT_atlas_DOT_de
Jan 9 '06 #2
On Mon, 09 Jan 2006 11:24:35 +0100, Stefan Näwe <pl****@nospam.net>
wrote:
Floogle wrote:
I cannot figure out why the following program instantiates two type
"2" instances instead of one "1" and one "2".

Could someone plz enlighten me?

Many thanks

Here's the code

-------------------------------------------------------------------------------
#include <iostream>
using namespace std;

class Base
{
public:

virtual int GetType()const=0;
};

class Dev1 : public Base
{
public:

int GetType()const{return 1;}
};

class Dev2 : public Base
{
public:

int GetType()const{return 2;}
};

struct BaseHolder
{
Base* pBase;

BaseHolder(Base* pB):pBase(pB)
{}
};

template <class T>
BaseHolder CreateABaseHolder()
{
return BaseHolder(new T());
}
int main()
{
BaseHolder h1 = CreateABaseHolder<Dev1>();
BaseHolder h2 = CreateABaseHolder<Dev2>();

cout << "\nID of base in h1 (should be == 1) = " <<
h1.pBase->GetType();
cout << "\nID of base in h2 (should be == 2)= " <<
h2.pBase->GetType();

return 0;
}
-------------------------------------------------------------------------


I get this:
$ ./baseholder

ID of base in h1 (should be == 1) = 1
ID of base in h2 (should be == 2) = 2
On which planet did you compile this?

/S

On planet MSVC6
Jan 9 '06 #3
Floogle wrote:
On planet MSVC6


http://msdn.microsoft.com/vstudio/ex...c/default.aspx

It's free!

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Jan 9 '06 #4
Floogle wrote:
On Mon, 09 Jan 2006 11:24:35 +0100, Stefan Näwe <pl****@nospam.net>
wrote:

Floogle wrote:
I cannot figure out why the following program instantiates two type
"2" instances instead of one "1" and one "2".

Could someone plz enlighten me?

Many thanks

Here's the code

-------------------------------------------------------------------------------
#include <iostream>
using namespace std;

class Base
{
public:

virtual int GetType()const=0;
};

class Dev1 : public Base
{
public:

int GetType()const{return 1;}
};

class Dev2 : public Base
{
public:

int GetType()const{return 2;}
};

struct BaseHolder
{
Base* pBase;

BaseHolder(Base* pB):pBase(pB)
{}
};

template <class T>
BaseHolder CreateABaseHolder()
{
return BaseHolder(new T());
}
int main()
{
BaseHolder h1 = CreateABaseHolder<Dev1>();
BaseHolder h2 = CreateABaseHolder<Dev2>();

cout << "\nID of base in h1 (should be == 1) = " <<
h1.pBase->GetType();
cout << "\nID of base in h2 (should be == 2)= " <<
h2.pBase->GetType();

return 0;
}
-------------------------------------------------------------------------


I get this:
$ ./baseholder

ID of base in h1 (should be == 1) = 1
ID of base in h2 (should be == 2) = 2
On which planet did you compile this?

/S


On planet MSVC6


OK.
I initially tried that with GCC 3.4.4 from Cygwin.

The test with MSVC gave me:

$ ./baseholder.exe

ID of base in h1 (should be == 1) = 2
ID of base in h2 (should be == 2) = 2
I'm confused, too!
It's probably one more of those annoying MSVC stupidities...
/S
--
Stefan Naewe
naewe.s_AT_atlas_DOT_de
Jan 9 '06 #5
On Mon, 09 Jan 2006 11:05:13 +0000, Ben Pope
<be*************@gmail.com> wrote:
Floogle wrote:
On planet MSVC6


http://msdn.microsoft.com/vstudio/ex...c/default.aspx

It's free!

Ben Pope

Alas, it's not that simple. It has to work using MSVC6... :o(
Jan 9 '06 #6
I'm confused, too!
It's probably one more of those annoying MSVC stupidities...


Lokks like a bug rather than a stupidity.
Jan 9 '06 #7
Floogle wrote:
On Mon, 09 Jan 2006 11:05:13 +0000, Ben Pope
<be*************@gmail.com> wrote:
Floogle wrote:
On planet MSVC6

http://msdn.microsoft.com/vstudio/ex...c/default.aspx

It's free!

Ben Pope

Alas, it's not that simple. It has to work using MSVC6... :o(


It never is. I have an MSVC6 program, having spent the time to port it
to MSVC2005 it now behaves "strangely".

With MSVC6 you are restricting your template abilities considerably...
Perhaps you can poke around within boost code for MSVC6 workarounds.

Good luck.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Jan 9 '06 #8
Gernot Frisch wrote:
I'm confused, too!
It's probably one more of those annoying MSVC stupidities...

Lokks like a bug rather than a stupidity.


It's a stupidity when you can get around it:

<----CODE---->

#include <iostream>
using namespace std;

class Base
{
public:
virtual ~Base() { }
virtual int GetType()const=0;
};

class Dev1 : public Base
{
public:

int GetType()const{return 1;}
};

class Dev2 : public Base
{
public:

int GetType()const{return 2;}
};

struct BaseHolder
{
Base* pBase;

BaseHolder(Base* pB):pBase(pB)
{}
};

template<typename T>
struct TypeHolder
{
typedef T OriginalType;
};

template <class T>
BaseHolder CreateABaseHolder(TypeHolder<T>)
{
return BaseHolder(new T());
}

int main()
{
BaseHolder h1 = CreateABaseHolder(TypeHolder<Dev1>());
BaseHolder h2 = CreateABaseHolder(TypeHolder<Dev2>());

cout << "\nID of base in h1 (should be == 1) = " << h1.pBase->GetType();
cout << "\nID of base in h2 (should be == 2) = " << h2.pBase->GetType();

return 0;
}

<----/CODE---->

/S
--
Stefan Naewe
naewe.s_AT_atlas_DOT_de
Jan 9 '06 #9

"Floogle" <Ba**********@NOSPAM.com> skrev i meddelandet
news:fg********************************@4ax.com...
On Mon, 09 Jan 2006 11:05:13 +0000, Ben Pope
<be*************@gmail.com> wrote:
Floogle wrote:
On planet MSVC6


http://msdn.microsoft.com/vstudio/ex...c/default.aspx

It's free!

Ben Pope

Alas, it's not that simple. It has to work using MSVC6... :o(

It is a known bug in VC6, that function templates that don't use the
template as a function parameter will confuse the linker. If you have
several overloads, one of them will be used (at random).

template <class T>
BaseHolder CreateABaseHolder()
{
return BaseHolder(new T());
}

doesn't work (as you have noticed).

The solution is to add a dummy parameter of type T, to generate a
different internal name.

template <class T>
BaseHolder CreateABaseHolder(const T* dummy = 0)
{
return BaseHolder(new T());
}

will work!
Bo Persson

Jan 9 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Elven | last post by:
Hi! I was trying to find the solution to this problem, but I don't think I could quite come up with the correct keywords to find it, since I'm pretty sure it's been asked before. In short,...
4
by: Grey Plastic | last post by:
I have several classes that all keep track of static data. However, the manner that they keep track of static data is identical, and so I'm using the template<class Child> class Parent { ... };...
31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
2
by: JT | last post by:
I have a compiler error when using a non-dependent type declared in a template, if I use the type in function definitions. I think it is a parsing issues related to confusion with a typename. ...
8
by: vpadial | last post by:
Hello, I want to build a library to help exporting c++ functions to a scripting languagge. The scripting language provides a function to register functions like: ANY f0() ANY f1(ANY) ANY...
11
by: Johan | last post by:
Hi Can somebody explain to me why I get this warning message and how I can solve this warning message. Thanks a lot Johan In member function `void
16
by: christopher diggins | last post by:
It appears that the following is not considered a class: template<typename T> class C { }; ? So officially is this considered: a class, a template, a class template, or a template class? I...
3
by: Erik Wikström | last post by:
I've been trying for a while now to understand how template template parameters work. But I just can't wrap my head around it and was hoping that someone might help me. As best I can figure the...
14
by: SoilMan | last post by:
Consider the following: class xyz { public: template <typename T> void foo(T x) { cout << "foo<T> " << x << endl; }
4
by: Jason | last post by:
/* I have never use template before, so bear with me. Here is what I am trying to do: I declare an abstract base class MatrixInterface with template to define an interface for all my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.