Topic: Deducing class template parameters
Author: dave@boost-consulting.com (David Abrahams)
Date: Sat, 1 May 2004 16:52:45 +0000 (UTC) Raw View
kprateek88@yahoo.com (Prateek R Karandikar) writes:
>> I haven't seen this idea before, but I like it very much. It makes
>> perfect sense, would simplify much ordinary and library code, and
>> would make for much nicer library interfaces in many cases. I
>> encourage you to write a language extension proposal for the
>> committee. One thing you ought to consider is how to specify what
>> happens when ctors are overloaded, e.g.:
>>
>> template <class T1, class T2>
>> struct pair
>> {
>> pair(T1, T2);
>>
>> template <class U1, class U2>
>> pair(U1, U2);
>>
>> // What if we add this one?
>> pair(int, T2);
>> };
>>
>> Cheers,
>> --
>> Dave Abrahams
>> Boost Consulting
>> http://www.boost-consulting.com
>
> Non-ctor template functions can be overloaded and the template
> arguments are deduced. So why not ctors? As a possible implementation,
> a compiler could generate a separate non-member function for every
> ctor to do do the deduction. ( Similar to make_pair, except that the
> *compiler* can generates its own _MAKE_pair and call it each time
> template parameter deduction is to be done, instead of the programmer
> writing such a function.)
That answer leaves out so many details that I don't know how to
interpret it. What are the precise non-member functions that would
be generated by the above declaration?
My sense is that templated ctors probably can't be called in that way
at all. I'm not sure about the 3rd overload above, but it seems as
though you can't deduce T1 in that case. Maybe
pair<double>("foo")
could call the 2nd ctor (for pair<double,char const*)).
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: kprateek88@yahoo.com (Prateek R Karandikar)
Date: Sat, 24 Apr 2004 15:59:19 +0000 (UTC) Raw View
===================================== MODERATOR'S COMMENT:
Please don't overquote.
===================================== END OF MODERATOR'S COMMENT
dave@boost-consulting.com (David Abrahams) wrote in message news:<u3c706i6a.fsf@boost-consulting.com>...
> kprateek88@yahoo.com (Prateek R Karandikar) writes:
>
> > Deducing class template parameters
> >
> > Why aren't class template parameters deduced from constructor calls?
> > Overloading functions with the same template-parameter-list is
> > allowed.
> > Constructors could be treated just like "normal" functions for this
> > purpose. In the absence of deduction of class template parameters, we
> > have to
> > unnecessarily use a separate function (make_pair for pair, not1 for
> > unary_negate, not2 for binary_negate, etc) to do the deduction. While
> > it is true that constructors tend to be overloaded a lot more than
> > other functions, I don't think this should influence the rules for
> > template parameter deduction.
>
> I haven't seen this idea before, but I like it very much. It makes
> perfect sense, would simplify much ordinary and library code, and
> would make for much nicer library interfaces in many cases. I
> encourage you to write a language extension proposal for the
> committee. One thing you ought to consider is how to specify what
> happens when ctors are overloaded, e.g.:
>
> template <class T1, class T2>
> struct pair
> {
> pair(T1, T2);
>
> template <class U1, class U2>
> pair(U1, U2);
>
> // What if we add this one?
> pair(int, T2);
> };
>
> Cheers,
> --
> Dave Abrahams
> Boost Consulting
> http://www.boost-consulting.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://www.jamesd.demon.co.uk/csc/faq.html ]
Non-ctor template functions can be overloaded and the template
arguments are deduced. So why not ctors? As a possible implementation,
a compiler could generate a separate non-member function for every
ctor to do do the deduction. ( Similar to make_pair, except that the
*compiler* can generates its own _MAKE_pair and call it each time
template parameter deduction is to be done, instead of the programmer
writing such a function.)
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
Author: kprateek88@yahoo.com (Prateek R Karandikar)
Date: Mon, 19 Apr 2004 16:08:29 +0000 (UTC) Raw View
Deducing class template parameters
Why aren't class template parameters deduced from constructor calls?
Overloading functions with the same template-parameter-list is
allowed.
Constructors could be treated just like "normal" functions for this
purpose. In the absence of deduction of class template parameters, we
have to
unnecessarily use a separate function (make_pair for pair, not1 for
unary_negate, not2 for binary_negate, etc) to do the deduction. While
it is true that constructors tend to be overloaded a lot more than
other functions, I don't think this should influence the rules for
template parameter deduction.
Has this been discussed before. I Google searched "deducing class
template parameters" on comp.std.c++ and didn't get any results.
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
Author: dave@boost-consulting.com (David Abrahams)
Date: Mon, 19 Apr 2004 21:45:39 +0000 (UTC) Raw View
kprateek88@yahoo.com (Prateek R Karandikar) writes:
> Deducing class template parameters
>
> Why aren't class template parameters deduced from constructor calls?
> Overloading functions with the same template-parameter-list is
> allowed.
> Constructors could be treated just like "normal" functions for this
> purpose. In the absence of deduction of class template parameters, we
> have to
> unnecessarily use a separate function (make_pair for pair, not1 for
> unary_negate, not2 for binary_negate, etc) to do the deduction. While
> it is true that constructors tend to be overloaded a lot more than
> other functions, I don't think this should influence the rules for
> template parameter deduction.
I haven't seen this idea before, but I like it very much. It makes
perfect sense, would simplify much ordinary and library code, and
would make for much nicer library interfaces in many cases. I
encourage you to write a language extension proposal for the
committee. One thing you ought to consider is how to specify what
happens when ctors are overloaded, e.g.:
template <class T1, class T2>
struct pair
{
pair(T1, T2);
template <class U1, class U2>
pair(U1, U2);
// What if we add this one?
pair(int, T2);
};
Cheers,
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: Michiel.Salters@logicacmg.com (Michiel Salters)
Date: Tue, 20 Apr 2004 18:02:47 +0000 (UTC) Raw View
kprateek88@yahoo.com (Prateek R Karandikar) wrote in message news:<607f883e.0404190623.3a4f4f2e@posting.google.com>...
> Deducing class template parameters
>
> Why aren't class template parameters deduced from constructor calls?
> Overloading functions with the same template-parameter-list is
> allowed.
> Constructors could be treated just like "normal" functions for this
> purpose. In the absence of deduction of class template parameters, we
> have to
> unnecessarily use a separate function (make_pair for pair, not1 for
> unary_negate, not2 for binary_negate, etc) to do the deduction. While
> it is true that constructors tend to be overloaded a lot more than
> other functions, I don't think this should influence the rules for
> template parameter deduction.
I don't think it is the #1 reason, but one-argument ctors compete with
conversion operators in other classes. That is, X(Y()) could use
X::X(Y) or Y::operator X().
Another reason is that I'm not sure whether it is possible to come up
with reasonable semantics. Every instantiation can have multiple
constructors. The actual number might differ, as classes do have
partial specialization. We need to select the best ctor from a
complex and usually infinite set of ctors.
Regards,
Michiel Salters
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
Author: hyrosen@mail.com (Hyman Rosen)
Date: Tue, 20 Apr 2004 20:48:57 +0000 (UTC) Raw View
David Abrahams wrote:
> One thing you ought to consider is how to specify what
> happens when ctors are overloaded
You also have to specify what happens when class templates
are specialized.
It seems to me that the way to go is to transform the ensemble
of constructors for all specializations of a class template
(including the base unspecialized one, if any) into a set of
overloaded functions and function templates, transform the
constructor call into a call to the overload set, see what's
chosen unambiguously, and transform back.
I'm not willing to do the work, though :-) make_pair is simpler.
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]