🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1865

Re: Template instantiation question

#1865
From: bnr (n.) postmaster
Date: Tue, 29 Aug 1995 15:11
66 lines
2910 bytes
In article <41b09t$ltj@engnews2.eng.sun.com>,
Steve Clamage <clamage@Eng> wrote:
>In article 95Aug21100336@physics10.Berkeley.EDU, bmcphers@alias.com (Brent McPherson) writes:
>>I have seen template classes that define methods that will only *work*
>>if the instantiation type provides certain methods/functionality.
>>[...]
>
>The draft standard, 14.3.2 Point of Instantiation [temp.point], says
>the implementation shall not instantiate a class or function that does not
>require instantiation. A non-virtual function that is never called and
>never has its address taken does not require instantiation.
>
>In your example, if you create a variable of type Foo<X>, where X is
>some type, and never use the non-virtual compare function, that
>function, Foo<X>::compare, should not be instantiated.
>
>Details of template definition and instantiation have been modified or
>clarified recently, and it will take a while for compilers to catch up
>to what is in the draft standard. 

Okay, but what happens if it isn't a function that doesn't exist...

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>"?

If so, is there any possible way to avoid "sizeof(void) is illegal" errors
from current compilers?


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.  Because of this, I can create a variable
of "MyPtrClass<MyStruct>", but not "MyPtrClass<int>".  While this is
almost exactly like the "compare" example that started this thread, in
this case the requirement for "T::operater->" is a language
requirement rather than something written in code.

[ 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: <41vamo$i3d@engnews2.Eng.Sun.COM>
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage
References: <41b09t$ltj@engnews2.eng.sun.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage
From: "bnr (n.) postmaster" <postmast@bnr.ca>
Newsgroups: comp.std.c++
Subject: Re: Template instantiation question
Date: 29 Aug 1995 15:11:20 GMT
Organization: Bell-Northern Research Ltd.
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <41vamo$i3d@engnews2.Eng.Sun.COM>
References: <41b09t$ltj@engnews2.eng.sun.com>
NNTP-Posting-Host: taumet.eng.sun.com
Content-Type: text
X-Nntp-Posting-Host: bcarsd26.bnr.ca 
Content-Length: 2086
X-Lines: 62
Originator: clamage@taumet