Topic: Implicitly finding out the template arguments for instantiating class templates
Author: sgganesh@gmail.com (Ganesh)
Date: Fri, 18 Jun 2004 14:59:35 +0000 (UTC) Raw View
>I have no illusion of it being easy, let alone even practical.
> :) My posting was more an attempt to hear about what issues there may
> be, and I'm grateful for the response.
>
> > If you think otherwise, try your hand at writing down the rules
> > and see how far you get.
>
I agree, but I don't think it would be as complex as implementing few
other template features already present in C++ ('export' comes to my
mind).
> I'm also inclined to think make_pair is simpler. :)
I am also inclined to think that template features described in ARM is
simpler ;-)
(now I see the power and expressiveness of template features that were
introduced when standardization was done).
-Ganesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: Michiel.Salters@logicacmg.com (Michiel Salters)
Date: Mon, 21 Jun 2004 19:29:52 +0000 (UTC) Raw View
Hyman Rosen <hyrosen@mail.com> wrote in message news:<1087401751.859488@master.nyc.kbcfp.com>...
> kanze@gabi-soft.fr wrote:
> > Hyman Rosen <hyrosen@mail.com> wrote
> > > Because constructor arguments don't have to have anything to do
> > > with template paramaters, and because you can have multiple
> > > overloaded constructors and constructor templates.
> >
> > The same holds for functions.
>
> Functions don't have two sets of template parameters that need
> to both be deduced. It's common for class templates to have
> templated constructors. I don't see how the rules for ordinary
> function template argument deduction carry over into this case,
> but I'm willing to be shown.
Why does it matter where a template parameter comes from?
Basically, in
template< typename CLASS >
class X {
template < typename CTOR >
X( CLASS, CTOR ) { }
}
}
we can deduce both parameters, CLASS and CTOR, for a given pair of
arguments. The only real problem is finding the complete set of
ctors.
Regards,
Michiel Salters.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: tslettebo@hotmail.com (Terje Sletteb?)
Date: Mon, 21 Jun 2004 19:29:52 +0000 (UTC) Raw View
damicha@sandia.gov (David A. Michael) wrote in message news:<9125fd79.0406181527.131ff987@posting.google.com>...
> I think all of this is just syntactic sugar for typeof. Correct me if
> I'm wrong, but I think you can do a lot of this with function
> templates and typeof...
> > > > std::complex c(1.0); // std::complex<double>
> template <class T>
> std::complex<T> make_complex(T t_val) {
> return std::complex<T>(t_val);
> }
> typedef typeof(make_complex(1.0)) my_complex;
> my_complex c(1.0);
Yes, and it can be done even simpler with the "auto" proposal I showed
in that posting, as quoted below here.
Since neither "typeof" nor "auto"/"decltype" is in the current
language ("decltype" has the same function as "typeof"), and since the
latter ones exist as a real proposal, I used them in the example.
> > auto p(make_pair(1,1.0));
> >
> > By the way, why not be able to name the deduced type:
> >
> > auto pair_type p(make_pair(1,1.0));
> typedef typeof(make_pair(1,1.0)) pair_type;
> pair_type p(make_pair(1, 1.0));
Sure, but with the "typeof" solution, you have to state the expression
twice, a needless and error-prone thing.
> Of course, we don't really have typeof yet either, but I've heard it
> has a good shot of getting in to the next standard. Anyone know?
See the link to the proposal I gave. I think there's a general feeling
that _some_ kind of type deduction feature will be in C++0x, whatever
form it will take.
After all, sizeof already does what typeof would have to do (deduce
the type), except that it doesn't make that information available.
> If we had it, we wouldn't need these other suggested features.
As mentioned, "decltype" is a version of "typeof", so the proposal is
a superset of "typeof".
> Or, if we
> really want them, maybe typeof would help us define their semantics,
> using basically the ideas above.
The detailed semantics of them is described in the proposal, as well
as a brief history of "typeof".
Regards,
Terje
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: tslettebo@hotmail.com (Terje Sletteb?)
Date: Mon, 14 Jun 2004 21:23:58 +0000 (UTC) Raw View
David Abrahams <dave@boost-consulting.com> wrote in message news:<uy8muysjh.fsf@boost-consulting.com>...
> tslettebo@hotmail.com (Terje Sletteb?) writes:
>
> > wolfgang@meijer.de (Wolfgang Meyer) wrote in message news:<6311ef2e.0406090440.4996bfc6@posting.google.com>...
> > > sgganesh@india.hp.com (Ganesh) wrote in message news:<71ea7138.0406072255.6e9d5032@posting.google.com>...
> > >
> > > >... Is there a way to
> > > > provide some mechanism, so that types can be inferred implicitly for
> > > > class template instantiations?
> >
> > This might be more appropriate for comp.std.c++ (so it's
> > cross-posted), but is there some fundamental reason why template
> > parameter deduction can't be done for class templates? I know it isn't
> > in the current language, but could it be?
> >
> > std::complex c(1.0); // std::complex<double>
>
> Someone posted this very idea recently. All such ideas are nice at
> first, but there are issues lurking, including those of overload
> resolution. Until someone does a thorough treatment of the issues and
> looks at the required standard changes, we won't know if there's "some
> fundamental reason why template parameter deduction can't be done for
> class templates". It would be cool, and you could be the guy who
> makes it possible, Terje!
Thanks for the confidence. :)
I've read up on the thread you mentioned, now
(http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=607f883e.0404190623.3a4f4f2e%40posting.google.com).
I've been away about a year from newsgroups and mailing lists
(including Boost), because of much work, and have only recently
started to catch up, again. I could have done a search before
cross-posting. Sorry about that.
There are indeed a lot of issues. Partial or no deducible parameters
(depending on the available constructors) means that, at least, there
has to be a mechanism for allowing some or all of the template
parameters to be provided explicitly.
For starters, there's the example you had in that thread:
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);
};
Even without the third overload, there's already ambiguity if we allow
"pair p(1,1.0)" to be used: Should it use constructor #1 to deduce the
template parameters, or should it use #2, and claim the template
parameters are nondeducible?
Hm, maybe it's just as well to use something like the following,
instead:
auto p(make_pair(1,1.0));
:)
("Decltype and auto"
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1607.pdf)
By the way, why not be able to name the deduced type:
auto pair_type p(make_pair(1,1.0));
Perhaps not much use for it?
Regards,
Terje
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: tslettebo@hotmail.com (Terje Sletteb?)
Date: Wed, 16 Jun 2004 15:45:03 +0000 (UTC) Raw View
Hyman Rosen <hyrosen@mail.com> wrote in message news:<Bfryc.16168$mz.15021@nwrdny02.gnilink.net>...
> Terje Sletteb? wrote:
> > This might be more appropriate for comp.std.c++ (so it's
> > cross-posted), but is there some fundamental reason why template
> > parameter deduction can't be done for class templates?
>
> Because constructor arguments don't have to have anything to do
> with template paramaters
True. Also:
pair p(1,1.0);
goes against the normal syntax of having "<type> <variable>(...)", as
the deduced type is _not_ "pair" (which is not even a type, but a
template).
> and because you can have multiple
> overloaded constructors and constructor templates. Writing the
> rules for how to deduce template parameters from such an overload
> set would, I think, be hideously difficult.
Yeah. You touch upon this in the other thread I've now read up on, a
couple of months ago, also (which I unfortunately hadn't searched for
first). I have no illusion of it being easy, let alone even practical.
:) My posting was more an attempt to hear about what issues there may
be, and I'm grateful for the response.
> If you think otherwise, try your hand at writing down the rules
> and see how far you get.
Actually, I think you summed it up quite well, here:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=K8%25gc.7379%242v.4470%40nwrdny02.gnilink.net
I'm also inclined to think make_pair is simpler. :)
Regards,
Terje
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: kanze@gabi-soft.fr
Date: Wed, 16 Jun 2004 15:45:03 +0000 (UTC) Raw View
Hyman Rosen <hyrosen@mail.com> wrote in message
news:<Bfryc.16168$mz.15021@nwrdny02.gnilink.net>...
> Terje Sletteb? wrote:
> > This might be more appropriate for comp.std.c++ (so it's
> > cross-posted), but is there some fundamental reason why template
> > parameter deduction can't be done for class templates?
> Because constructor arguments don't have to have anything to do
> with template paramaters, and because you can have multiple
> overloaded constructors and constructor templates.
The same holds for functions.
> Writing the
> rules for how to deduce template parameters from such an overload
> set would, I think, be hideously difficult.
The same holds for functions:-).
> If you think otherwise, try your hand at writing down the rules
> and see how far you get.
It should be rather trivial; just copy the applicable rules for
functions, and apply them to constructors.
The fun comes, of course, when you start trying to put the two
together:-). Should make overload resolution even more fun.
--
James Kanze GABI Software
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S mard, 78210 St.-Cyr-l' cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: Hyman Rosen <hyrosen@mail.com>
Date: Wed, 16 Jun 2004 20:27:10 +0000 (UTC) Raw View
kanze@gabi-soft.fr wrote:
> Hyman Rosen <hyrosen@mail.com> wrote
> > Because constructor arguments don't have to have anything to do
> > with template paramaters, and because you can have multiple
> > overloaded constructors and constructor templates.
>
> The same holds for functions.
Functions don't have two sets of template parameters that need
to both be deduced. It's common for class templates to have
templated constructors. I don't see how the rules for ordinary
function template argument deduction carry over into this case,
but I'm willing to be shown.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: David Abrahams <dave@boost-consulting.com>
Date: Sun, 13 Jun 2004 19:01:54 +0000 (UTC) Raw View
tslettebo@hotmail.com (Terje Sletteb?) writes:
> wolfgang@meijer.de (Wolfgang Meyer) wrote in message news:<6311ef2e.0406090440.4996bfc6@posting.google.com>...
> > sgganesh@india.hp.com (Ganesh) wrote in message news:<71ea7138.0406072255.6e9d5032@posting.google.com>...
> >
> > >... Is there a way to
> > > provide some mechanism, so that types can be inferred implicitly for
> > > class template instantiations?
> >
> > I guess you are already aware of it: but instead of just instantiating
> > the template, your helper function could return an instance of the
> > instantiated template. That is, you provide a non-member constructor
> > as std::make_pair does for std::pair:
> >
> > template<typename T1, typename T2>
> > contain<T1, T2> make_contain(const T1& t1, const T2& t2)
> > {
> > return contain<T1, T2>( t1, t2 );
> > }
>
> This might be more appropriate for comp.std.c++ (so it's
> cross-posted), but is there some fundamental reason why template
> parameter deduction can't be done for class templates? I know it isn't
> in the current language, but could it be?
>
> std::complex c(1.0); // std::complex<double>
>
> In the case where insufficient information may be passed to the
> constructor to deduce the type, explicit template parameters can be
> used, just as with function templates:
>
> std::complex<double> c;
>
> Thoughts?
Someone posted this very idea recently. All such ideas are nice at
first, but there are issues lurking, including those of overload
resolution. Until someone does a thorough treatment of the issues and
looks at the required standard changes, we won't know if there's "some
fundamental reason why template parameter deduction can't be done for
class templates". It would be cool, and you could be the guy who
makes it possible, Terje!
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: ta0kira@yahoo.com (ta0kira)
Date: Mon, 14 Jun 2004 15:18:05 +0000 (UTC) Raw View
tslettebo@hotmail.com (Terje Sletteb?) wrote in message
news:<b0491891.0406101221.f534252@posting.google.com>...
> std::complex c(1.0); // std::complex<double>
>
> In the case where insufficient information may be passed to the
> constructor to deduce the type, explicit template parameters can be
> used, just as with function templates:
>
> std::complex<double> c;
My personal opinion is that if you are creating an object, you should know what
type it is, therefore you should use the template parameter. I can understand
using it for functions since your use of the function may be miles away from the
declaration of the object being used in it, but when you are actually creating
the object, you have to declare a type (or part of a type in the case of your
suggestion) anyway. And what about templatized constructors, constructors with
arguments that aren't the same type as the template argument, partially
specialized classes with a constructor that takes an arg of the param type and
one that takes a different type arg, etc.? And suppose you use a reference in
the constructor; will the template be a 'Type', a 'Type&', a 'const Type&', or a
'const Type'? Sounds convenient in some cases, but it would leave a lot of room
for errors that would be hard to track down. I think the few cases that it
would make things easier are fractional compared to the limitless ambiguity it
would cause when misused. Also, someone trying to read your code would have to
track the initializing argument back to its own creation, which would maybe lead
to another wild goose chase to find the type it was initialized with, all just
to figure out what type you intended to use when you could have just used a
template argument. Also, it would give the appearance that a template is not
even being used, thereby confusing the reader more when they realize that the
objects they thought were the same type are actually completely different.
//------------------------------------------------------------------------------
template <class Type>
struct BASE
{
BASE(const Type&);
template <class Type2>
BASE(const Type2&);
};
template <class Type>
struct BASE <Type&>
{
BASE(const Type&);
};
//------------------------------------------------------------------------------
//... (meanwhile) ...
//------------------------------------------------------------------------------
int main() //Your suggested way looks simple, but...:
{
int VALUE1;
double VALUE2;
BASE ONE(VALUE1); //What constructor is called?
BASE TWO(VALUE2); // " "
BASE THREE(ONE); //What type is 'THREE'?
//... (lots and lots of code) ...
BASE FOUR(THREE); //I will never figure out what type 'FOUR' is...
FOUR = TWO; //Is this legal?
}
//------------------------------------------------------------------------------
//... (alternate) ...
//------------------------------------------------------------------------------
int main() //The current way is complex looking, but...:
{
int VALUE1;
double VALUE2;
BASE <int&> ONE(VALUE1); //I can figure out which constructor is used
BASE <int> TWO(VALUE2); // " "
BASE <BASE <int&> > THREE(ONE); //I know exactly what type 'THREE' is
//... (lots and lots of code) ...
BASE <BASE <BASE <int&> >& > FOUR(THREE); //And this one too...
//FOUR = TWO; //Definitely not legal...
}
//------------------------------------------------------------------------------
ta0kira
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do 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://www.jamesd.demon.co.uk/csc/faq.html ]