Topic: Two (advanced?) template questions


Author: Don Griffin <dgriffin@no.spam.farallon.com>
Date: 1997/09/02
Raw View
David,

> >     template <class T>
> >     class foo
> >     {
> >         T f () { }
> >         T g () { return f(); }
> >     };
> >
> >     foo<void>  f;
> >     f.g();  // OK! Wow!
>
> I think this was made valid at the last ISO/ANSI meeting
> (the intent to make it valid was expressed in the prior
>  meeting).
>

I cannot tell you how happy that would make me :)  It couldn't be too
difficult for the compiler to transform "return f()" into "f();
return;"...

Sadly, VC++5 does not actually accept this code.  What made me think
that it did was that everything compiled when I said "foo<void>".  It
broke when I later called "f.g()".  Sorry for the misinfo...


> > Is this correct behavior?  This only makes sense because the template
> > could not know if it would be instantiated with "void".  It would be
> > impossible to code a template that could take void or non-void w/o the
> > behavior of allowing "return f()" to compile.
>
> Not impossible. Just a little less convenient (use
> specialization or partial specialization).

I'll give you the "not impossible" part.  Maybe you'll compromise on the
"little less convenient" part?  I would say "much less convenient"
<G>...

Thanks for the info.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: David Vandevoorde <daveed@vandevoorde.com>
Date: 1997/08/31
Raw View
Don Griffin wrote:
[...]
> #1
>
>     template <class T>
>     class foo
>     {
>         T f () { }
>         T g () { return f(); }
>     };
>
>     foo<void>  f;
>     f.g();  // OK! Wow!

I think this was made valid at the last ISO/ANSI meeting
(the intent to make it valid was expressed in the prior
 meeting).

> Is this correct behavior?  This only makes sense because the template
> could not know if it would be instantiated with "void".  It would be
> impossible to code a template that could take void or non-void w/o the
> behavior of allowing "return f()" to compile.

Not impossible. Just a little less convenient (use
specialization or partial specialization).

> This does *not* compile:
>
>     void x () { }
>     void y () { return x(); }  // Error: void func returning a value

I'm not positive, but I think the recent change would
also cover this.

> I assume that the compiler turned "return f()" into "f(); return;"
> when T = void.  Then "return x()" must not be allowed because the
> developer knows full well that y() has a void return type?
>
> #2
>
>     template <class T, class U>
>     class foo : public base
>     {
>         ...
>     };
>
>     template <class U>
>     class foo<void,U> : public different_base
>     {
>         ...
>     };
>
> This partial specialization does not compile, and I get the error:
>
>     foo<void,`template-parameter-1'>' : template class has already been
>     defined as a non-template class
>
> Obviously, this is an incorrect error message since both foo's are
> template classes.  The on-line help indicates that specialization is
> supported.  Is partial specialization not supported?

Specialization and partial specialization are extremely
different (also from an implementer's point-of-view): e.g.,
a partial specialization is a template, whereas a (full)
specialization is not.

Your compiler may not support partial specialization yet.
Try HP or EDG-based (e.g., SGI, KAI) compilers for this
stuff.

 Daveed
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: "Paul D. DeRocco" <pderocco@ix.netcom.crud.com>
Date: 1997/08/31
Raw View
Don Griffin wrote:
>
> I have noted some odd behavior from VC++ 5 and I was wondering if the
> compiler is doing things correctly.
>
> #1
>
>     template <class T>
>     class foo
>     {
>         T f () { }
>         T g () { return f(); }
>     };
>
>     foo<void>  f;
>     f.g();  // OK! Wow!
>
> Is this correct behavior?  This only makes sense because the template
> could not know if it would be instantiated with "void".  It would be
> impossible to code a template that could take void or non-void w/o the
> behavior of allowing "return f()" to compile.  This does *not* compile:
>
>     void x () { }
>     void y () { return x(); }  // Error: void func returning a value
>
> I assume that the compiler turned "return f()" into "f(); return;" when
> T = void.  Then "return x()" must not be allowed because the developer
> knows full well that y() has a void return type?

As far as I know, the former is incorrect, and I also wish it were
correct, even if it required that the latter be deemed correct as well.
In fact, I sent a proposal to that effect to the standards committee. In
some cases relating to functors, it cuts in half the number of templates
that must be written.

--

Ciao,
Paul

(Please remove the extra "crud" from the return address,
which has been altered to foil junk mail senders.)
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]