Topic: proposal: class template template


Author: "Geurt Vos" <G.Vos@rohill.nl>
Date: Wed, 28 Feb 2001 08:24:48 GMT
Raw View
>
> > template <typename T, T value> class C {};
>
> > Why can't the compiler decide for me what the type for 'T' is when I
> > pass 'value'? Specifying the type for T seems apart from annoying
> > quite redundant.
>
> What compiler are you using?  Borland's should support this just fine.
>

Huh? I hope you misunderstood me :) This template is supported,
but now *I* have to specify a type for T, whereas I think there's
enough detail available to the compiler when I pass only 'value'
for it to decide the type for 'T'. I mean, I don't want to say stuff
like:

    C<int,100> c;

I simply want to say:

    C<100> c;

This can't be done with the given or any other template...or am I
missing something here?

Geurt



---
[ 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://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: John Wiegley <jwiegley@inprise.com>
Date: Wed, 28 Feb 2001 19:53:04 GMT
Raw View
>>>>> On Wed Feb 28, Geurt writes:

> I simply want to say:

>     C<100> c;

> This can't be done with the given or any other template...or am I
> missing something here?

You sure this doesn't work?  The type of 100 is int, and T will be
deduced from.......

Oh wait, I think I'm seeing now.  You can't say "T value" unless
you've had an earlier declaration for T.  Doh.

Hmm... yes, I see what you mean.  You could always write a template
function to return allocated objects to you, since T doesn't have to
be explicit specified in that case.  It wouldn't work for stack and
data objects, but it would for heap.

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: "Victor Bazarov" <vAbazarov@dAnai.com>
Date: Thu, 1 Mar 2001 06:43:15 GMT
Raw View
"Geurt Vos" <G.Vos@rohill.nl> wrote...
>
> >
> > > template <typename T, T value> class C {};
> >
> > > Why can't the compiler decide for me what the type for 'T' is when
I
> > > pass 'value'? Specifying the type for T seems apart from annoying
> > > quite redundant.
> >
> > What compiler are you using?  Borland's should support this just
fine.
> >
>
> Huh? I hope you misunderstood me :) This template is supported,
> but now *I* have to specify a type for T, whereas I think there's
> enough detail available to the compiler when I pass only 'value'
> for it to decide the type for 'T'. I mean, I don't want to say stuff
> like:
>
>     C<int,100> c;
>
> I simply want to say:
>
>     C<100> c;
>
> This can't be done with the given or any other template...or am I
> missing something here?

So, what you're suggesting is the ability to give a template a wild
card and have some sort of "type_of" construct to actually return
the type of the expression given, right?

template<typename T value> class C
{
    // I don't know how you're going to use this but
    // it probably could look like
    T& reference;
public:
    C() : reference(value) {}
};

class ABC;

...
    C<42> has_int; // declares and initializes 'reference' to
                   // refer to a temporary 42
    ABC   abc;
    C<abc> has_abc; // declares and initializes 'reference' to
                    // refer to the abc object?

And the compiler should know that since 42 is an int, it creates
the template which has T as 'int'.  And since 'abc' has type ABC
it creates the instance which has T as 'ABC'...

Did I get it right?  What would be the use of such thing?

Victor
--
Please remove capital A's from my address when replying by mail



---
[ 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://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: "Geurt Vos" <G.Vos@rohill.nl>
Date: Fri, 2 Mar 2001 00:08:32 GMT
Raw View
> So, what you're suggesting is the ability to give a template a wild
> card and have some sort of "type_of" construct to actually return
> the type of the expression given, right?
>
> template<typename T value> class C
> {
>     // I don't know how you're going to use this but
>     // it probably could look like
>     T& reference;
> public:
>     C() : reference(value) {}
> };
>
> class ABC;
>
> ...
>     C<42> has_int; // declares and initializes 'reference' to
>                    // refer to a temporary 42
>     ABC   abc;
>     C<abc> has_abc; // declares and initializes 'reference' to
>                     // refer to the abc object?
>
> And the compiler should know that since 42 is an int, it creates
> the template which has T as 'int'.  And since 'abc' has type ABC
> it creates the instance which has T as 'ABC'...
>
> Did I get it right?

Almost. That is, I think you get the idea, but the implementation
is illegal. For one the reference inside C<> will require the
compiler to create a temporary, for which it simply has no obvious
place to store it. Other than that, passing 'abc' is illegal - twice.
1) you can only pass compile-time constants as non-type template
args, and 2) non-type template parameters can't be of floating
point, class or void type, and for pointers they require external
linkage. I'm not proposing to change this (make it possible), I'm
only proposing an additional construct.

> What would be the use of such thing?

For one it's a lazy thing. That is, I don't want to do what a compiler can
do for me. This is, however, not the main reason. Consider a variation
to the original template:

template <typename T, typename T::X value>
struct C {};

T::X is a pointer type. Now what should be possible is to pass 0
as value, to indicate no pointer is passed. One compiler requires
that '0' is static_cast to T::X, and another compiler states it can't
be done claiming '0' doesn't have external linkage (doesn't it?
doesn't seem to make much sense to me...). Both problems would
have been solved if the compiler would figure out the types for me.
Then when '0' is passed, the type is 'int', after which I can do some
compile-time type checking (and possibly value checking) to
determine whether a pointer value or 0 is passed.

I know a reasonable way around it...but if C++ were to support
class template templates, it would make it all far clearer...

Geurt




---
[ 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://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]