Topic: New Standard questions
Author: Matt Austern <austern@isolde.mti.sgi.com>
Date: 1997/11/25 Raw View
"Paul D. DeRocco" <pderocco@ix.netcom.com> writes:
> 1) Does the standard allow an implementation to validate the semantics of a
> complete template whenever it is instantiated, or does it require that it only
> validate the semantics of the particular components that are used? In
> particular, is it guaranteed that a template that defines an operator->() (such
> as auto_ptr) will work on a non-class type, as long as the user doesn't use ->
> on it, or is the implementation allowed to barf when it sees this?
The answer to this is slightly complicated---not because there's
anything tricky or ill-defined, but just becuase there are several
different questions here all of which sound similar.
The short answer: if you don't use a member function, then the
compiler isn't supposed to instantiate it. (Note that several popular
compilers get this wrong.) In particular, you may define a member
function operator-> and you may instantiate the class for a non-class
type, so long as operator-> isn't actually used. The standard C++
library relies on this fact.
For the full answer, or at least a fuller answer, I think I'll just
cut and paste from a mail message I sent to someone else who asked
more or less the same thing. (The section and paragraph numbers refer
to the pre-Morristown version of the working paper.)
(1) If you declare a template and instantiate it, and the instantiaion
is illegal in some way, then it is an error and a diagnostic is
required.
[14.3, paragraph 6: "If the use of a template-argument gives rise to an
ill-formed construct in the instantiation of a template specialization,
the program is ill-formed."]
(2) If you declare a template and don't instantiate it, then it is an error
if there's no possible instantiation that would be legal. However, no
diagnostic is required. (That is, you don't have to do syntax checking
of uninstantiated templates.)
[14.6, paragraph 7. "No diagnostic shall be issued for a template
definition for which a valid specialization can be generated. If no
valid specialization can be generated for a template definition, and that
template is not instantiated, the template definition is ill-formed, no
diagnostic required."]
(3) If you instantiate a class template, then you do not automatically
instantiate all of its members. You instantiate a member only if
you use it in a way that requires instantiation---for example, if
you call it or you take its address.
[14.7.1, paragraph 9. "An implementation shall not implicitly
instantiate a function template, a member template, a non-virtual
member function, a member class or a static data member of a class
template that does not require instantiation."]
---
[ 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 ]
Author: stephen.clamage_nospam@eng.sun.com (Steve Clamage)
Date: 1997/11/27 Raw View
On 25 Nov 97 14:02:20 GMT, fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
wrote:
>"Paul D. DeRocco" <pderocco@ix.netcom.com> writes:
>
>>2) Does the standard allow a return statement to return a value, if the
>>function return type is void? Does it allow it if the value is cast to =
void
>>first? This issue comes up in templates occasionally.
>
>Definitely yes to the second question. I think the answer to the first
>one is also yes, but I'm not sure off-hand.
The answer to the first question is no. From 6.6.3 "Return statement":
"A return statement with an expression of non=ADvoid type can be used
only in functions returning a value; ..."
---
Steve Clamage, stephen.clamage_nospam@eng.sun.com
( Note: remove "_nospam" when replying )
---
[ 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 ]
Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1997/11/24 Raw View
1) Does the standard allow an implementation to validate the semantics of a
complete template whenever it is instantiated, or does it require that it only
validate the semantics of the particular components that are used? In
particular, is it guaranteed that a template that defines an operator->() (such
as auto_ptr) will work on a non-class type, as long as the user doesn't use ->
on it, or is the implementation allowed to barf when it sees this?
2) Does the standard allow a return statement to return a value, if the
function return type is void? Does it allow it if the value is cast to void
first? This issue comes up in templates occasionally.
--
Ciao,
Paul
---
[ 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 ]
Author: herbs@cntc.com (Herb Sutter)
Date: 1997/11/25 Raw View
On 24 Nov 97 11:36:00 GMT, "Paul D. DeRocco" <pderocco@ix.netcom.com> wrote:
>1) Does the standard allow an implementation to validate the semantics of a
>complete template whenever it is instantiated, or does it require that it only
>validate the semantics of the particular components that are used?
The latter. Furthermore, if a templated function has a default value, the
implementation may not evaluate the default value's legality until/unless it
is actually used. For example (modulo typos as I write this):
template<typename T> struct X {
void f( const T& = T() ) {} // default value uses T's default ctor
};
struct Y { X(int) {} }; // Y has no default ctor
X<Y> value; // ok
value.f(2); // ok, not using default value
The implementation is not permitted to diagnose an error, since the default
value is never used. If we instead wrote "value.f();" it would be an error,
since the default value cannot be constructed because Y does not have a
default ctor.
>In
>particular, is it guaranteed that a template that defines an operator->() (such
>as auto_ptr) will work on a non-class type, as long as the user doesn't use ->
>on it, or is the implementation allowed to barf when it sees this?
I didn't quite follow that, but if the user never uses the template's
operator->() then it must never be evaluated (or even generated, I think).
>2) Does the standard allow a return statement to return a value, if the
>function return type is void? Does it allow it if the value is cast to void
>first? This issue comes up in templates occasionally.
Since London, "void f() { return void; }" is well-formed for exactly that
reason.
---
[ 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 ]
Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/11/25 Raw View
"Paul D. DeRocco" <pderocco@ix.netcom.com> writes:
>1) Does the standard allow an implementation to validate the semantics of a
>complete template whenever it is instantiated, or does it require that it only
>validate the semantics of the particular components that are used?
The latter.
>In particular, is it guaranteed that a template that defines an
>operator->() (such as auto_ptr) will work on a non-class type, as long
>as the user doesn't use -> on it, or is the implementation allowed to
>barf when it sees this?
That idiom is guaranteed to work.
>2) Does the standard allow a return statement to return a value, if the
>function return type is void? Does it allow it if the value is cast to void
>first? This issue comes up in templates occasionally.
Definitely yes to the second question. I think the answer to the first
one is also yes, but I'm not sure off-hand.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ 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 ]