Topic: The trouble with templates (and the solution?)


Author: zalman@netcom.com (Zalman Stern)
Date: 1999/03/19
Raw View
nbecker@fred.net wrote:
: I wonder if a hybrid approach would be possible.  Keep the current
: template design, but add the option of specifying the requirements of
: type parameters explicitly.  In other words, besides saying:

: template<class T>
: blah blah...

: I can specify a signature that T must satisfy (what methods or operators).

You can pretty much do this already by adding methods to the template class
you are defining which call the methods or operators you are planning to
use. E.g.

template <class T>
class Example {
 T accumulator;
protected:
 void must_have_operator_plus() { T test; test + test; };
}

There is a small amount of name pollution within the class scope, but that
should be manageable.

Otherwise, you will quickly find yourself asking for a full macro language
that runs at compile time. While a typesafe hygienic macro language based
on templates might be a very cool thing, I expect it will take years to
specify.

-Z-
---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Gabriel Dos_Reis <gdosreis@korrigan.inria.fr>
Date: 1999/03/19
Raw View
nbecker@fred.net writes:

| I find c++ templates to be enormously useful.  Generic programming
| fits wonderfully into my favorite domain, namely signal processing and
| communications.
|
| But I believe the current template design could be improved.
| Templates and generic programming run counter to c++ tradition -
| namely, strong typing.

I'm puzzled here.

| ...  With templates the coder does not specify
| explicitly anything about the requirements of types.  Instead, the
| required interface is implicit in the code.  This is more in the style
| of untyped, dynamic languages (e.g., python).

The situation is slighly different: type checking is done at
instantiation time not runtime.

| I wonder if a hybrid approach would be possible.  Keep the current
| template design, but add the option of specifying the requirements of
| type parameters explicitly.  In other words, besides saying:

This issue is a recurrent one; you might probably want to take a look
at

 "Design and Evolution of C++"
 Bjarne Stroustrup, Addison-Wesley

--
Gabriel Dos Reis, dosreis@cmla.ens-cachan.fr
---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Stephen.Clamage@Eng.Sun.COM (Steve Clamage)
Date: 1999/03/19
Raw View
nbecker@fred.net writes:

>I find c++ templates to be enormously useful.  Generic programming
>fits wonderfully into my favorite domain, namely signal processing and
>communications.

>But I believe the current template design could be improved.
>Templates and generic programming run counter to c++ tradition -
>namely, strong typing.  With templates the coder does not specify
>explicitly anything about the requirements of types.  Instead, the
>required interface is implicit in the code.  This is more in the style
>of untyped, dynamic languages (e.g., python).

>I wonder if a hybrid approach would be possible.  Keep the current
>template design, but add the option of specifying the requirements of
>type parameters explicitly.  In other words, besides saying:

>template<class T>
>blah blah...

>I can specify a signature that T must satisfy (what methods or operators).

Both D&E and C++PL have a discussion of this very point, with
an explanation of why C++ does not have the capability.

--
Steve Clamage, stephen.clamage@sun.com
---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1999/03/19
Raw View
Zalman Stern wrote:
>
> nbecker@fred.net wrote:
> : I wonder if a hybrid approach would be possible.  Keep the current
> : template design, but add the option of specifying the requirements of
> : type parameters explicitly.  In other words, besides saying:
>
> : template<class T>
> : blah blah...
>
> : I can specify a signature that T must satisfy (what methods or operators).
>
> You can pretty much do this already by adding methods to the template class
> you are defining which call the methods or operators you are planning to
> use. E.g.
>
> template <class T>
> class Example {
>         T accumulator;
> protected:
>         void must_have_operator_plus() { T test; test + test; };
> }

That achieves the desired effect only if the code using Example<>
actually calls must_have_operator_plus(). Only the member functions
actually used (directly or indirectly) get instantiated, which is
actually fairly convenient. The designer of a utility template can
provide a set of member functions that cover all the possible needs for
the template, without having to worry that some of them won't be
instantiatable for particular values of the template argument - with
good design, the members that aren't instiatable for a given argument
are the ones that won't be needed with that argument.

A prime example is the existence of various STL member functions that
take default arguments of the type T(). The standard requires that
classes described by the standard as having members with default
arguments (for the sake of simplicity) such as:

 template <class T> struct simple{
  T t;
  simple(const T &in_t=T()) : t(in_t) {};
 };

should actually be declared as:

 template <class T> struct simple{
  T t;
  simple() : t(T()) {};
  simple(const T &in_t) : t(in_t) {};
 };

The reason for doing this is that because the constructors are split,
the first one isn't instantiated unless used. That means that it's legal
to instantiate 'simple<myclass>', even if 'myclass' has no accessible
default constructor, so long as no attempt is made to use the particular
members of 'simple' that require it. There are frequent messages on this
newsgroup due to MSVC++ 5.0's failure to correctly implement this.


[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: zalman@netcom.com (Zalman Stern)
Date: 1999/03/20
Raw View
James Kuyper (kuyper@wizard.net) wrote:
: That achieves the desired effect only if the code using Example<>
: actually calls must_have_operator_plus(). Only the member functions
: actually used (directly or indirectly) get instantiated, which is
: actually fairly convenient.

Cool. So the only reason to want this is to provide better error messages
for common programming mistakes where you "know" that a certain operator
will be used.

template <class T>
class Example {
public:
    T accumulator;

    /* Provide a better error message. */
    inline void must_have_int_assign(int value) { accumulator = value; }
    inline size_t must_have_plus() { return sizeof(accumulator + 0); }

    Example() { must_have_int_assign(0); must_have_operator_plus(); }
    /* A bunch of methods all of which require operator+ */
}

(Yeah, I'm half joking. Maybe 5 years from now, compiler developers will
provide template errors so that a competent programmer is better off
reading the error message than going directly to the template and the
instantiation and figuring out what's wrong from first principles...)

-Z-


[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: nbecker@fred.net
Date: 1999/03/18
Raw View
I find c++ templates to be enormously useful.  Generic programming
fits wonderfully into my favorite domain, namely signal processing and
communications.

But I believe the current template design could be improved.
Templates and generic programming run counter to c++ tradition -
namely, strong typing.  With templates the coder does not specify
explicitly anything about the requirements of types.  Instead, the
required interface is implicit in the code.  This is more in the style
of untyped, dynamic languages (e.g., python).

I wonder if a hybrid approach would be possible.  Keep the current
template design, but add the option of specifying the requirements of
type parameters explicitly.  In other words, besides saying:

template<class T>
blah blah...

I can specify a signature that T must satisfy (what methods or operators).

This is analogous to the evolution of K&R C -> ANSI C -> C++ with
respect to function parameters.


[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]