Article View: comp.std.c++
Article #3624Re: 'const' in header files
From: Tony Bass
Date: Fri, 08 Mar 1996 17:14
Date: Fri, 08 Mar 1996 17:14
70 lines
3042 bytes
3042 bytes
>From article <4hn54s$3am@engnews1.Eng.Sun.COM>, by clamage@Eng.Sun.COM (Steve Clamage):
[... other interesting discussion]
> I suppose I should also comment on the comparison with unneeded template
> instantiations. That isn't an optimization issue, but a correctness issue.
> Simple example:
> template< class T > class X {
> T t;
> ...
> bool operator<( const T& rhs ) { return t < rhs.t; }
> };
> Suppose we instantiate X on a type T for which the "<" operator is not
> defined. That isn't a problem unless we invoke X<T>::operator<. So the
> language rule is that a compiler can't reject a program due to an
> invalid instantiation unless the instantiation is actually required.
[...]
Presumably this language rule also covers the situation below with an
incomplete type, and my C++ compiler (HP-UX cfront-based) is incorrect
to reject it?
I had an example where I needed only pointers,
template<struct T> struct VBlock;
struct VScratchRep;
struct VScratch {
VScratchRep *chunk;
VBlock<VScratchRep> *free;
};
int main()
{
VScratch *p = (VScratch *)0;
return 0;
}
which the compiler would accept in isolation, but if during the same
compilation it saw the definition of VBlock<T>::operator[],
template<struct T> struct VBlock {
T &operator[](int i);
T *v;
};
template<struct T> inline T &VBlock<T>::operator[](int i)
{
return v[i];
}
it failed, needing to know the size of VScratchRep even though [] was
never called in this compilation. (I discovered this when including two
header files while compiling something else.)
Tony Bass
--
# Tony Bass Tel: (01473) 645305
# MLB 3/19, BT Laboratories e-mail: aeb@saltfarm.bt.co.uk
# Martlesham Heath, Ipswich, Suffolk, IP5 7RE DO NOT e-mail to From: line
# Opinions are my own
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Technical Details
Message-ID:
<4hpd36$m42@calf.saltfarm.bt.co.uk>Path:
arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austernReferences:
<4hn54s$3am@engnews1.Eng.Sun.COM>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern From: aeb@saltfarm.bt.co.uk (Tony Bass) Newsgroups: comp.std.c++ Subject: Re: 'const' in header files Date: 08 Mar 1996 09:14:15 PST Organization: BT Speech Technology Section Approved: austern@isolde.mti.sgi.com Message-ID: <4hpd36$m42@calf.saltfarm.bt.co.uk> References: <4hn54s$3am@engnews1.Eng.Sun.COM> NNTP-Posting-Host: isolde.mti.sgi.com X-Original-Date: 8 Mar 1996 13:35:34 -0000 Posted-Date: Fri, 8 Mar 1996 13:35:35 GMT X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBMUBq/Uy4NqrwXLNJAQEkGgH+MJmH5pR9tbj5tKKsw05harICTKHQoSf4 Mkfh+yOv69kCTbgVMMUhFsLgApfGn21ATKeJHJeeekVDM3XKN12mOQ== =3bow Originator: austern@isolde.mti.sgi.com