|
From: sasha on 10 Apr 2008 06:18 I am bit confused as to whether the following is allowed; why and why not: 1) template<typename T> class Foo{ .... virtual TsomeOp(); }; 2) template<typename T> class Foo{ .... template<typename K> virtual T someOp(K k); }; 3) class Foo{ .... template<typename K> virtual T someOp(K k); }; What are the rules governing virtual member of a template class, virtual template member of a template class and template member of non- template class? Thanks, Sasha -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Gerhard Menzl on 11 Apr 2008 02:33 sasha wrote: > 1) > template<typename T> > class Foo{ > > ... > virtual TsomeOp(); > > }; > > 2) > > template<typename T> > class Foo{ > > ... > template<typename K> > virtual T someOp(K k); > > }; > > 3) > > class Foo{ > > ... > template<typename K> > virtual T someOp(K k); > > }; > > What are the rules governing virtual member of a template class, > virtual template member of a template class and template member of non- > template class? The rules are quite simple: a class template can have virtual member functions, but virtual member functions can never be member templates. Hence, 1 is legal, but 2 and 3 are not. The technical reason is that a virtual member template would make it very hard to determine the size of the virtual function table. The number of entries could only be calculated after analyzing the entire program, which conflicts with C++'s separate compilation model. Virtual functions of class templates impose no such difficulty. -- Gerhard Menzl Non-spammers may respond to my email address, which is composed of my full name, separated by a dot, followed by at, followed by "fwz", followed by a dot, followed by "aero". [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Thomas Maeder on 11 Apr 2008 06:32 sasha <aborovinsky(a)gmail.com> writes: > I am bit confused as to whether the following is allowed; why and why > not: > > 1) > template<typename T> > class Foo{ > > ... > virtual TsomeOp(); > > }; Allowed > 2) > > template<typename T> > class Foo{ > > ... > template<typename K> > virtual T someOp(K k); > > }; Not allowed > 3) > > class Foo{ > > ... > template<typename K> > virtual T someOp(K k); > > }; Not allowed > What are the rules governing virtual member of a template class, Allowed (if the member function isn't a template), just like virtual member functions of non-template classes. > virtual template member of a template class Not allowed, because nobody knows how these would be implemented efficiently. > and template member of non-template class? Allowed if non-virtual. I think your actual question is: virtual template member of non-template class , which is not allowed for the same reason that virtual template member of a template classes aren't. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
Pages: 1 Prev: std::complex and extensible literals. Next: typename keyword |