Topic: Implicitly finding out the template arguments for instantiating


Author: damicha@sandia.gov (David A. Michael)
Date: Sun, 20 Jun 2004 14:41:29 +0000 (UTC)
Raw View
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);


 > 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));

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?  If
we had it, we wouldn't need these other suggested features.  Or, if we
really want them, maybe typeof would help us define their semantics,
using basically the ideas above.


      [ 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: Mon, 14 Jun 2004 15:18:05 +0000 (UTC)
Raw View
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. Writing the
rules for how to deduce template parameters from such an overload
set would, I think, be hideously difficult.

If you think otherwise, try your hand at writing down the rules
and see how far you get.


      [ 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: Fri, 11 Jun 2004 15:09:34 +0000 (UTC)
Raw View
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?

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                       ]