 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jeff Paciga Guest
|
Posted: Sat Feb 21, 2004 10:48 am Post subject: Template Instantiation of Polymorphic Types |
|
|
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
|
Posted: Sat Feb 21, 2004 4:25 pm Post subject: Re: Template Instantiation of Polymorphic Types |
|
|
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
|
Posted: Sun Feb 22, 2004 1:48 am Post subject: Re: Template Instantiation of Polymorphic Types |
|
|
"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
|
Posted: Sun Feb 22, 2004 10:53 am Post subject: Re: Template Instantiation of Polymorphic Types |
|
|
| 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 |
|
 |
|
|
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
|
|