C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Template Instantiation of Polymorphic Types

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Jeff Paciga
Guest





PostPosted: Sat Feb 21, 2004 10:48 am    Post subject: Template Instantiation of Polymorphic Types Reply with quote



Hi,

Using Microsoft Visual C++ 2003, the following code compiles, as expected.

template <class T>
struct Singleton
{
static T& GetInst()
{
static T t;
return t;
}
};

template <class T>
struct Classy : public Singleton<Classy
{
/* virtual */ void F() { gjgjg = gkgk; }
};

template <>
struct Classy<std::string> : public Singleton<Classy
{
void F() {}
};

template <class T>
void G(T t, Classy<T>* p = &Classy<T>::GetInst())
//void G(T t, Classy<T>* p = 0)
{
p->F();
}

int main ()
{
G<std::string>("hello");
}

The non-specialised version of Classy intentionally contains garbage,
but that is fine in this example because it doesn't get instantiated.
However, if I make Classy<T>::F virtual, the example fails to compile
because it tries to instantiate Classy<const char*>, even though,
as far as I can tell, it doesn't need to.

Changing the default value of the second parameter of G from
&Classy<Y>::GetInst() to 0 fixes the problem; the generic version
of Classy is never instantiated.

Can anyone tell me why making Classy polymorphic causes Classy<const char*>
to be instantiated?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Francis Glassborow
Guest





PostPosted: Sat Feb 21, 2004 4:25 pm    Post subject: Re: Template Instantiation of Polymorphic Types Reply with quote



In message <2b432c4f.0402201333.7eef9094 (AT) posting (DOT) google.com>, Jeff
Paciga <jeffp (AT) flashmail (DOT) com> writes
Quote:
Can anyone tell me why making Classy polymorphic causes Classy to be instantiated?

At a guess because a virtual function is always deemed to be used and
therefore, in a template context, instantiated (the compiler needs to
complete the vtable)


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jonathan Turkanis
Guest





PostPosted: Sun Feb 22, 2004 1:48 am    Post subject: Re: Template Instantiation of Polymorphic Types Reply with quote



"Jeff Paciga" <jeffp (AT) flashmail (DOT) com> wrote

Quote:
Hi,

Using Microsoft Visual C++ 2003, the following code compiles, as
expected.

template <class T
struct Singleton
{
static T& GetInst()
{
static T t;
return t;
}
};

template struct Classy : public Singleton {
/* virtual */ void F() { gjgjg = gkgk; }
};

template
struct Classy {
void F() {}
};

template void G(T t, Classy //void G(T t, Classy<T>* p = 0)
{
p->F();
}

int main ()
{
G<std::string>("hello");
}

I believe VC7.1 is making two mistakes here. First, I think your code
should not compile even with 'virtual' commented out: 'gjgjg' and
'gkgk' are non-dependent, and should be looked up right away. (I tried
this on several compilers, but only Comeau 4.3.3 complained.)
Qualifying these names with 'this.' should take care of this.

VC7.1 is also wrong to attempt to instantiate Classy<const char*>;
template argument deduction shouldn't occur at all here, IMO, since
the argument is explcitly specified. I can't find a non-microsoft
compiler that has this problem.

Jonathan







[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jeff Paciga
Guest





PostPosted: Sun Feb 22, 2004 10:53 am    Post subject: Re: Template Instantiation of Polymorphic Types Reply with quote

Quote:
At a guess because a virtual function is always deemed to be used and
therefore, in a template context, instantiated (the compiler needs to
complete the vtable)

But why with T=const char* ? It is as though it is trying to deduce
the template parameter when instantiating the class, even though I
specify std::string when I call G. (If I pass a const T& to G
instead, it tries to instantiate with const char[6]... if I cast
the string literal to std:string, it compiles fine.)

In any case, I am thinking this is just an idiosyncrasy of this
particular compiler. I will try to read some more about template
instantiation rules... Thanks.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.