Article View: comp.std.c++
Article #1870Re: Template instantiation question
From: Steve Clamage
Date: Tue, 29 Aug 1995 18:18
Date: Tue, 29 Aug 1995 18:18
64 lines
2662 bytes
2662 bytes
In article i3d@engnews2.Eng.Sun.COM, "bnr (n.) postmaster" <postmast@bnr.ca> writes:
>
>template<class T>
>class Foo {
>public:
> // etc...
> int Bar(void)
> {
> DoSomething( sizeof(T) );
> // ^^^^^^^^^
> }
> // etc...
>};
>
>This will compile file with any type 'T' except "void". Does the new
>draft allow this to be defined as long as I don't call "Bar()" on anything
>of type "Foo<void>"?
I believe that if you never invoke or take the address of Foo<void>::Bar(),
and if Bar is not virtual, that the template should be valid. Some current
compilers might reject the code, but I believe it is allowed by the
draft standard. As I noted earlier, the draft standard says that template
functions that do not need to be generated must not be generated; the
compiler should thus never see the invalid use of sizeof.
>If so, is there any possible way to avoid "sizeof(void) is illegal" errors
>from current compilers?
Declare a specialization in the template header Foo.h:
int Foo<void>::Bar();
The compiler should not attempt to generate Bar for Foo<void>.
>Along a similar vein...
>
>template<class T>
>class MyPtrClass {
> T* ptr;
>public:
> // etc...
> T* operator->(void) { return(ptr); }
> // etc...
>};
>
>
>This causes problems on some compilers (e.g. Borland C++ v4.5) if type
>'T' is does not have "operator->" defined, even if I never call the
>"operator->" defined above.
I believe the template declaration is valid only when T is a class type.
Section 13.5.6 "Class Member Access" places requirements on the return type
of operator->. When the class template is processed, the declaration would be
invalid even if you never use the function.
Possibly someone could point out something in the draft which makes the
template valid as long is operator-> is never actually referenced.
---
Steve Clamage, stephen.clamage@eng.sun.com
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<41vlmh$kat@engnews2.Eng.Sun.COM>Path:
arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamageShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage From: stephen.clamage@Eng.sun.com (Steve Clamage) Newsgroups: comp.std.c++ Subject: Re: Template instantiation question Date: 29 Aug 1995 18:18:57 GMT Organization: Sun Microsystems, Inc. Approved: clamage@eng.sun.com (comp.std.c++) Message-ID: <41vlmh$kat@engnews2.Eng.Sun.COM> NNTP-Posting-Host: taumet.eng.sun.com Content-Type: text X-Nntp-Posting-Host: taumet.eng.sun.com Content-Length: 1886 X-Lines: 60 Originator: clamage@taumet