[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lmi] C++11 extern template

From: Greg Chicares
Subject: Re: [lmi] C++11 extern template
Date: Wed, 8 Feb 2017 19:01:08 +0000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Icedove/45.6.0

On 2017-02-08 16:39, Vadim Zeitlin wrote:
> On Wed, 8 Feb 2017 11:29:38 +0000 Greg Chicares <address@hidden> wrote:
> GC> On 2017-02-07 14:37, Vadim Zeitlin wrote:
> ..
> GC> >  BTW, this would, again, be irrelevant anyhow if you follow my idea of
> GC> > using a private template ctor or a function, but I thought to use 
> another
> GC> > C++ feature I had never used before, even though it appeared in C++11,
> GC> > called "explicit instantiation declarations" a.k.a. "extern templates". 
> I
> GC> > believed I could prevent implicit instantiation from compiling in this 
> way,
> GC> > but it looks like I misunderstood how this worked because the code using
> GC> > InputSequence ctor from std::vector<bool> still compiled just fine -- 
> even
> GC> > though it failed to link, of course. Now I wonder what exactly extern
> GC> > templates are useful for...
> GC> 
> GC> I actually thought of that when I was writing the patch we're discussing
> GC> here, so I looked it up...and came away with the same question. I just
> GC> don't see any reason why I'd want to use them.
>  Just to get this out of the way: I understand this now, after reading
> http://stackoverflow.com/a/8131212/15275 (which is wrong, BTW, as pointed
> out in the comment by Johannes Schaub, but this doesn't diminish its
> pedagogical value). It's really just an optimization of compile-time which
> tells the compiler to not bother implicitly instantiating this particular
> template instance, but doesn't say anything at all about instantiating (and
> especially refusing to instantiate) any other instances of it. So it was
> definitely a mistake to think about it as I did.
>  But IMHO there is still something wrong with the explanation at
> http://en.cppreference.com/w/cpp/language/class_template if it create the
> same misunderstanding for both of us, so I'll think about proposing to
> change it to be more clear.

For the record, I think what raised my hopes was this, which is
perhaps brief to a fault:
whereas this:

| You should only use extern template to force the compiler to not
| instantiate a template when you know that it will be instantiated
| somewhere else.

clarified it for me. Hmmm...although apparently different, that is
the same stackoverflow article you cite.

Now let's consider
which I hadn't looked at before. I quickly read the beginning of
the long "Explicit instantiation" section, and...yes, I was confused,
but only because there's so little lexical distance between
  explicit instantiation definition  = e31n
  explicit instantiation declaration = e32n
It says that an explicit instantiation d8n forces instantiation, so
I guessed that an explicit instantiation d9n might do what I wanted.

But 8 != 9. I suppose that's my fault, but I wouldn't recommend
writing instructions for a defibrillator this way.

The article could be greatly improved by saying in plain language,
at the top and not in an addendum far below, that "extern" forces
the compiler *not* to instantiate. I don't need cppreference.com
to repeat the standard verbatim, because I always have the actual
standard open in a pdf reader.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]