9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Winston Kodogo <kodogo@gmail.com>
To: 9fans@9fans.net
Subject: Re: [9fans] 9fans Digest, Vol 103, Issue 52
Date: Sat, 24 Nov 2012 18:36:13 +1300	[thread overview]
Message-ID: <CAFiGbxgki2LCX2s6R-aKWG+Vvk9arQSXyJqeMngGgMZ7+f+MWA@mail.gmail.com> (raw)
In-Reply-To: <mailman.679.1353666467.2642.9fans@9fans.net>

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);



           reply	other threads:[~2012-11-24  5:36 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <mailman.679.1353666467.2642.9fans@9fans.net>]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAFiGbxgki2LCX2s6R-aKWG+Vvk9arQSXyJqeMngGgMZ7+f+MWA@mail.gmail.com \
    --to=kodogo@gmail.com \
    --cc=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).