From 3661410806904956229 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f78e5,8c2a5609551cacb7 X-Google-Attributes: gidf78e5,public From: fwai@armltd.co.uk (Francis Wai) Subject: Re: Instantiating members of templates. Date: 1996/11/13 Message-ID: <56c81k$420@sis.armltd.co.uk>#1/1 X-Deja-AN: 196226417 references: <328929bd.109078106@nntp.ix.netcom.com> x-original-date: 13 Nov 1996 10:31:16 GMT organization: Advanced RISC Machines Ltd x-auth: PGPMoose V1.1 PGP comp.std.c++ newsgroups: comp.std.c++ originator: fjh@mundook.cs.mu.OZ.AU miker3@ix.netcom.com (Mike Rubenstein) writes: >What is the intent of the following statement in 14.3.2: > An implementation shall not instantiate a function, nonvirtual > member function, class or member class that does not require > instantiation. >Am I correct in believing that this would make the following code >legal? > class A { > }; > template B { > public: > void f(const T& x) { x.foo(); } > }; > int main() > { > B b; > return 0; > } >My reasoning is that isnce B::f is never called, it will not be >instantiated and A::foo() need not be defined. No, this would not be legal. The crux of the matter is that 'x' is not regarded as a class/struct/union object by the compiler. Quoting from a DWP 14.7 clause 4, 4 A template-parameter that could be interpreted as either an parameter- declaration or a type-parameter (because its identifier is the name of an already existing class) is taken as a type-parameter. A template- parameter hides a variable, type, constant, etc. of the same name in the enclosing scope. [Example: class T { /* ... */ }; int i; template void f(T t) { T t1 = i; // template-arguments T and i ::T t2 = ::i; // globals T and i } Here, the template f has a type-parameter called T, rather than an unnamed non-type parameter of class T. ] There is no semantic differ- ence between class and typename in a template-parameter. Note the last sentence in the above paragraph. Again, quoting from D&E 15.3, The keyword _class_ is used to indicate arguments of type _type_ partly because it appears to be an appropriate word, partly because it saves introducing a new keyword. In this context, _class_ means "any type" and not just "some user-defined type". Quite a number of people have fallen into the trap believing the obvious. This is unfortunate. --Francis --- [ comp.std.c++ is moderated. To submit articles: try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ] [ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ] [ Comments? mailto:std-c++-request@ncar.ucar.edu ]