Topic: Deducing template parameters from constructor calls


Author: auwz <auwz@auwz.example.com>
Date: Mon, 1 Jun 2009 12:36:08 CST
Raw View
Faisal Vali wrote:

    Like i said, i see some potential here, and i feel it would be very
    desirable to be able to write:
    pair<> p(5,  string("foo");
    tuple<> t(5, 5.0, X<int,string>);

    But I sense (without much authority) that there are many nuances here
    that would need much more work.
    But if you are up to it, I would be very interested in seeing a full
    fledged proposal on this topic.


Thanks for your reply
Unfortunately I am far from having the knowledge required to create a
proposal on this (i.e. be able to analyze the nuances) :-(

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: auwz <auwz@auwz.example.com>
Date: Sat, 23 May 2009 11:59:08 CST
Raw View
SG wrote:
>
> No, it won't happen. To explain shirarenai's response a little more:

Ok thanks for your explanation SG, now it's clearer.

Acutally I asked the question because I really don't like the make_xxx
approach: it looks patchy and unprofessional to me. Also it is still
more verbose and more difficult to read than I hoped notwithstanding
the "auto".
In fact I was surprised when I first met the make_pair factory... Now
are we supposed to make a make_X thing for each X template class we
create? (and standard libraries! :-( )
Wasn't a create_X function *exactly* the purpose of the constructor of
the X class?

Wasn't it better to define a lookup behaviour? At least 2 come to my mind:

1) If there is ambiguity, like in your example --> compile error
2) Prefer the specialization. If there is still ambiguity among
specializations, go (1)

Why these approaches were not chosen?

Thank you

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Faisal Vali <faisalv@gmail.com>
Date: Sun, 24 May 2009 15:13:28 CST
Raw View
On May 23, 12:59 pm, auwz <a...@auwz.example.com> wrote:
> SG wrote:
>
<snip>
> Acutally I asked the question because I really don't like the make_xxx
> approach: it looks patchy and unprofessional to me. Also it is still
> more verbose and more difficult to read than I hoped notwithstanding
> the "auto".
> In fact I was surprised when I first met the make_pair factory... Now
> are we supposed to make a make_X thing for each X template class we
> create? (and standard libraries! :-( )

Well you can always write this generic creator in C++0x (which should
work for all cases where your template is parameterized on type
parameters alone):

template<template<class...> class TT, class ... Args>
TT<Args...> create(Args&& ... args)
{
 return TT<Args...>(args...);
}
template<class T> struct C {
 C(T) { }
};
int main()
{
 using namespace std;
 auto c = create<C>(create<pair>(5, string("foo")));
 auto c2 = C<pair<int,string>>(pair<int,string>(5,"foo"));  // the
alternative  (neither is great)
}

>
> Wasn't it better to define a lookup behaviour? At least 2 come to my mind:
>
It would be convenient to have a mechanism where you can bind
deduction of constructor parameters to the class templates
parameters.  But, like the other posters, I don't see an *easy*
solution in the setting of partial/explicit specializations.

regards,
Faisal Vali
Radiation Oncology
Loyola

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: SG <s.gesemann@gmail.com>
Date: Mon, 25 May 2009 10:55:02 CST
Raw View
On 24 Mai, 23:13, Faisal Vali <fais...@gmail.com> wrote:
>
> template<template<class...> class TT, class ... Args>
> TT<Args...> create(Args&& ... args)
> {
>  return TT<Args...>(args...);}

Interesting. I havn't seen an unnamed template parameter pack inside a
template template parameter before. (Is this really going to work?)

You seem to have forgotten perfect forwarding

  return TT<Args...>(std::forward<Args>(args)...);

Cheers!
SG


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Faisal Vali <faisalv@gmail.com>
Date: Tue, 26 May 2009 10:41:29 CST
Raw View
On May 25, 11:55 am, SG <s.gesem...@gmail.com> wrote:
> On 24 Mai, 23:13, Faisal Vali <fais...@gmail.com> wrote:
>
>
>
> > template<template<class...> class TT, class ... Args>
> > TT<Args...> create(Args&& ... args)
> > {
> >  return TT<Args...>(args...);}
>
> Interesting. I havn't seen an unnamed template parameter pack inside a
> template template parameter before. (Is this really going to work?)
<snip>

Yes. Refer to section 14.4.3 (n2857, p320).
Note that the second example in paragraph 14.4.3/2 is wrong.
(http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#744)

regards,
Faisal Vali
Radiation Oncology
Loyola



--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: auwz <auwz@auwz.example.com>
Date: Tue, 26 May 2009 11:43:28 CST
Raw View
Faisal Vali wrote:

        Wasn't it better to define a lookup behaviour? At least 2 come
to my mind:

    It would be convenient to have a mechanism where you can bind
    deduction of constructor parameters to the class templates
    parameters.  But, like the other posters, I don't see an *easy*
    solution in the setting of partial/explicit specializations.


Thank you, may I ask what is the problem that you see with the two
approaches (1) and (2) I proposed?

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Faisal Vali <faisalv@gmail.com>
Date: Sun, 31 May 2009 09:20:32 CST
Raw View
> Faisal Vali wrote:
>
>
>     It would be convenient to have a mechanism where you can bind
>     deduction of constructor parameters to the class templates
>     parameters.  But, like the other posters, I don't see an *easy*
>     solution in the setting of partial/explicit specializations.

On May 26, 12:43 pm, auwz <a...@auwz.example.com> wrote:
>
> Thank you, may I ask what is the problem that you see with the two
> approaches (1) and (2) I proposed?
>

> 1) If there is ambiguity, like in your example --> compile error
> 2) Prefer the specialization. If there is still ambiguity among
> specializations, go (1)

Well there is some potential here but I sense that the details would
need some work.

I am assuming you are proposing some syntax that would bind deduction
of default template params to ctor params:

template<class T1 = deduce_from_ctor, class T2 = deduce_from_ctor>
struct X
{
    X(T1 t1, T2 t2);
};

template<> struct X<int, string>
{
   X(int);
   X(int, string);
   X(string, int);
};

X<> x(5, string("foo"));
   - this would deduce to <int,string> using primary template's ctor
   - compiler would also find the <int,string> explicit specialization
     - would check the specialization for a ctor that takes an int and
string
     - if no such ctor, then error
     - else use the specialization

X<int,string> x(3); // ok - use explicit specialization
X<int,int> x(3,4); // ok  - use primary template

Like i said, i see some potential here, and i feel it would be very
desirable to be able to write:
pair<> p(5,  string("foo");
tuple<> t(5, 5.0, X<int,string>);

But I sense (without much authority) that there are many nuances here
that would need much more work.
But if you are up to it, I would be very interested in seeing a full
fledged proposal on this topic.

regards,
Faisal Vali
Radiation Oncology
Loyola





--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: auwz <auwz@auwz.example.com>
Date: Wed, 20 May 2009 17:13:37 CST
Raw View
Hi there,

the thing that functions can deduce their own template parameters from
the function call parameters but classes cannot do the same (from
constructor call) always annoyed me.

Is it going to change in C++0x? If not why?

I mean that I would like the following code to compile


template <typename T>
class C{
      C(const T & t) : t(t) {}
      T t;
};

int main(){
      int a=3;
      C c(a);
...
}


Thank you

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: shirarenai@yandex.ru
Date: Wed, 20 May 2009 23:52:03 CST
Raw View
What about partial and explicit specializations?

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: SG <s.gesemann@gmail.com>
Date: Thu, 21 May 2009 10:32:31 CST
Raw View
On 21 Mai, 01:13, auwz <a...@auwz.example.com> wrote:
> Hi there,
>
> the thing that functions can deduce their own template parameters from
> the function call parameters but classes cannot do the same (from
> constructor call) always annoyed me.
>
> Is it going to change in C++0x? If not why?
>
> I mean that I would like the following code to compile
>
> template <typename T>
> class C{
>       C(const T & t) : t(t) {}
>       T t;
> };
>
> int main(){
>       int a=3;
>       C c(a);
>       ...
> }

No, it won't happen. To explain shirarenai's response a little more:
You could have some specializations of the class template with totally
different constructors:

   template<>
   class C<double> {
     C(const int&);
   };

So you have at least two classes that accept a reference-to-const-int:
C<double> and C<int>. Note that this is different from function
template specialization because such a specialization can't change the
parameter types.

But you will be able to provide some "make_xxx" function (like
std::make_pair) and use 'auto' for type inferrence like this:

   template<typename T>
   C<T> make_c(const T& x) { return C<T>(x); }

   int main() {
     auto foo = make_c(3.14159265);
     // decltype(foo) --> C<double>
   }

Cheers!
SG


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]