9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* Re: [9fans] 9fans Digest, Vol 103, Issue 52
       [not found] <mailman.679.1353666467.2642.9fans@9fans.net>
@ 2012-11-24  5:36 ` Winston Kodogo
  0 siblings, 0 replies; only message in thread
From: Winston Kodogo @ 2012-11-24  5:36 UTC (permalink / raw)
  To: 9fans

Also, also, it was Gorka rather than Bakul who called me out for being
rude, so apologies to both.

But this is my favourite section from Herr Stroustrup's book - from
Section C.13.5 - although the following section (C.13.6 Template as a
Qualifier) is also good for a laugh. Right, I'll shut up and stop
trolling after this.

However, this does not allow us to write:
template<class C>void f(C&v) {
C::iterator i=v.begin();//error
//... }

Unfortunately, the compiler isn’t required to be psychic, so it
doesn’t know that C::iterator is the name of a type. In the previous
example, the compiler could look at the declaration of vector<> to
determine that the iterator in vector<T>::iterator was a type. That is
not possible when the qualifier is a type parameter. Naturally, a
compiler could postpone all checking until instantiation time where
all information is available and could then accept such examples.
However, that would be a nonstandard language extension.

Consider an example stripped of clues as to its meaning:

template<class T>void f(T&v) {
T::x(y); // error? }

Is T::x a function called with a nonlocal variable y as its argument?
Or, are we declaring a variable y with the type T::x perversely using
redundant parentheses? We could imagine a context in which X::x(y) was
a function call and Y::x(y) was a declaration.

The resolution is simple: unless otherwise stated, an identifier is
assumed to refer to something that is not a type or a template. If we
want to state that something should be treated as a type, we can do so
using the typename keyword:
template<class C>void f(C&v) {
typename C::iterator i=v.begin();
//... }
The typename keyword can be placed in front of a qualified name to
state that the entity named is a type. In this, it resembles struct
and class.
The typename keyword can also be used as an alternative to class in
template declarations. For example:
template<typename T>void f(T);

Being an indifferent typist and always short of screen space, I prefer
the shorter:
template<class T>void f(T);



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2012-11-24  5:36 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <mailman.679.1353666467.2642.9fans@9fans.net>
2012-11-24  5:36 ` [9fans] 9fans Digest, Vol 103, Issue 52 Winston Kodogo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).