Topic: Automatic detection of return type for template function


Author: nagle@animats.com (John Nagle)
Date: Sat, 25 Jan 2003 21:08:25 +0000 (UTC)
Raw View
Allan W wrote:

> nagle@animats.com (John Nagle) wrote
>
>>One of the more generally useful ideas in this area is
>>to provide "let", for usages such as
>>
>> let i = x; // declares i as having the same type as x.
....
>>
>>"Typeof" allows something similar:
>>
>> for(typeof(tab.begin) p = tab.begin(); p != tab.end(); p++)
>>
>>but it's uglier.
>>
>
> Both of these come up frequently. I support either one of them
> (I like "let" slightly better, but typeof() would work well enough.)


   Clearly, you'd want to write "let" in ordinary code.  "typeof"
is more suited to obscure uses inside templates.


>
> However, this *is* a standards newsgroup! Let's use the standard
> term "simple-declaration" rather than "operator". See 7.1 (dcl.dcl)
> and 13.5 (over.oper) to see why.


   I didn't write "operator".

     John Nagle
     Animats

---
[ 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: allan_w@my-dejanews.com (Allan W)
Date: Sat, 25 Jan 2003 02:27:33 +0000 (UTC)
Raw View
nagle@animats.com (John Nagle) wrote
> One of the more generally useful ideas in this area is
> to provide "let", for usages such as
>
>  let i = x; // declares i as having the same type as x.
>
> This is particularly useful for iterators, which have clunky
> syntax.  This would allow a very common form to be written concisely:
>
>  vector<int> tab;
>  for(let p=tab.begin(); p != tab.end(); p++)
>
> instead of having to write
>
>  for(vector<int>::iterator p = tab.begin(); p != tab.end(); p++)
>
> This is a common enough construct to be worth the trouble.
> It's also gets that explicit reference to the type name out of
> there.
>
> "Typeof" allows something similar:
>
>  for(typeof(tab.begin) p = tab.begin(); p != tab.end(); p++)
>
> but it's uglier.

Both of these come up frequently. I support either one of them
(I like "let" slightly better, but typeof() would work well enough.)

However, this *is* a standards newsgroup! Let's use the standard
term "simple-declaration" rather than "operator". See 7.1 (dcl.dcl)
and 13.5 (over.oper) to see why.

---
[ 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: allan_w@my-dejanews.com (Allan W)
Date: Wed, 22 Jan 2003 18:28:24 +0000 (UTC)
Raw View
> > philippe_mori@hotmail.com ("Philippe Mori") wrote
> > > I would also like to have the typeof operator that would give the
> > > type of an expression.
> > >
> > > And I would like for automatic type deduction for variable declaration:
> > >
> > >     typename T t = g();
> > >
> > > where T would be a type and t the variable. T could eventually be made
> > > optional if we do not need it afterward (and it does not complicated the
> > > parsing too much).

> Allan W wrote:
> > I would like that. A lot of people would.

Meaning, I would like to be able to use "typename" or "typeof" in
some way analagous to the code above.

> > But I think we should quit calling it the "typeof operator".
> >     std::cout << typeof(5);
> > Should this print "int"?

witless@attbi.com (Witless) wrote
> Only if:
>
>     std:cout << int;
>
> ... would print "int".

Right. It's nonsense.

typeof() cannot "return" anything. It isn't an operator.

An operator can return an int (5, or -22, or so on). Conceivably an
operator could return the string "int". What it cannot do is return
the type int. Returning a type doesn't make sense.

> > What we really want is syntax that lets us
> > deduce the type of a variable by it's initialization.

> No, we want typeof( expr ) to provide (not return) the appropriate type
> identifier.  You are confusing type deduction with type extraction.

No, I'm not.

All I'm saying is, "typeof" wouldn't be an operator, in the sense that
+, &, and sizeof are operators.

Was I really that unclear?

---
[ 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: nagle@animats.com (John Nagle)
Date: Wed, 22 Jan 2003 21:31:16 +0000 (UTC)
Raw View
    One of the more generally useful ideas in this area is
to provide "let", for usages such as

 let i = x; // declares i as having the same type as x.

This is particularly useful for iterators, which have clunky
syntax.  This would allow a very common form to be written concisely:

 vector<int> tab;
 for(let p=tab.begin(); p != tab.end(); p++)

instead of having to write

 for(vector<int>::iterator p = tab.begin(); p != tab.end(); p++)

This is a common enough construct to be worth the trouble.
It's also gets that explicit reference to the type name out of
there.

"Typeof" allows something similar:

 for(typeof(tab.begin) p = tab.begin(); p != tab.end(); p++)

but it's uglier.

    John Nagle
    Animats





Allan W wrote:

>>>philippe_mori@hotmail.com ("Philippe Mori") wrote
>>>
>>>>I would also like to have the typeof operator that would give the
>>>>type of an expression.

---
[ 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: musiphil@bawi.org (KIM Seungbeom)
Date: Mon, 20 Jan 2003 18:31:10 +0000 (UTC)
Raw View
Philippe Mori wrote:
>
> I would like to be able to let the compiler deduce the return type of a
> function without needing to declare it.
>
> template <typename T = typeof(return)> T f(int i, double d) { return i + d; }
>
> That is, the return type would be deduced from the return statement. I don't
> know what would be the best syntax for that but it could be really usefull
> to avoid having to explicitly declare the return type.

That would surely be useful.
Think of all the hacks that Andrei Alexandrescu had to go through
in his article "Min and Max Redivivus", simply to define useful
min and max functions!
With this feature, one could simply write:

    template<typename T, typename U, typename R=typeof(return)>
    const R& min(const T& t, const U& u) { return t < u ? t : u; }

We should come up with a better syntax probably, though.

--
KIM Seungbeom <musiphil@bawi.org>

---
[ 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: witless@attbi.com (Witless)
Date: Mon, 20 Jan 2003 18:31:44 +0000 (UTC)
Raw View
Allan W wrote:

> philippe_mori@hotmail.com ("Philippe Mori") wrote
> > I would like to be able to let the compiler deduce the return type of a
> > function without needing to declare it.
> >
> > template <typename T = typeof(return)> T f(int i, double d) { return i +
> > d; }
> >
> > That is, the return type would be deduced from the return statement. I don't
> > know what would be the best syntax for that but it could be really usefull
> > to avoid having to explicitly declare the return type.
>
>     int foo1(int i, double d) {
>         if (i) return i; // Returns int
>         else   return d; // Returns double converted to int
>     }
>     double foo2(int i, double d) {
>         if (i) return i; // Returns int converted to double
>         else   return d; // Returns double
>     }
>     deduceme foo3(int i, double d) {
>         if (i) return i; // Does it return an int?
>         else   return d; // Or does it return a double?
>     }
>
> > I would also like to have the typeof operator that would give the type of an
> > expression.
> >
> > And I would like for automatic type deduction for variable declaration:
> >
> >     typename T t = g();
> >
> > where T would be a type and t the variable. T could eventually be made
> > optional if we do not need it afterward (and it does not complicated the
> > parsing too much).
>
> A lot of people would like that. So would I. But I think we should quit
> calling it the "typeof operator".
>     std::cout << typeof(5);
> Should this print "int"?

Only if:

    std:cout << int;

... would print "int".

> What we really want is syntax that lets us
> deduce the type of a variable by it's initialization.

No, we want typeof( expr ) to provide (not return) the appropriate type
identifier.  You are confusing type deduction with type extraction.

---
[ 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: Ken@Alverson.net ("Ken Alverson")
Date: Mon, 20 Jan 2003 18:32:23 +0000 (UTC)
Raw View
"Allan W" <allan_w@my-dejanews.com> wrote in message
news:7f2735a5.0301171526.1279e308@posting.google.com...
>
>     deduceme foo3(int i, double d) {
>         if (i) return i; // Does it return an int?
>         else   return d; // Or does it return a double?
>     }

It seems to me, were something like this implemented, the return type
would be chosen in the same manner as the type of the ?: operator.

Ken


---
[ 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: rogero@howzatt.demon.co.uk ("Roger Orr")
Date: Mon, 20 Jan 2003 18:34:15 +0000 (UTC)
Raw View
"Allan W" <allan_w@my-dejanews.com> wrote in message
news:7f2735a5.0301171526.1279e308@posting.google.com...
> philippe_mori@hotmail.com ("Philippe Mori") wrote
> > I would like to be able to let the compiler deduce the return type of a
> > function without needing to declare it.
> >
> > template <typename T = typeof(return)> T f(int i, double d) { return i +
> > d; }
> >
> > That is, the return type would be deduced from the return statement. I
don't
> > know what would be the best syntax for that but it could be really
usefull
> > to avoid having to explicitly declare the return type.
>
>     deduceme foo3(int i, double d) {
>         if (i) return i; // Does it return an int?
>         else   return d; // Or does it return a double?
>     }
>
I don't think this need be an insurmountable problem - there are already
rules for deducing the overall expression type of the conditional operator.
So if I change your example to use the conditional operator:

deduceme foo3( int i, double d )
{
   return i ? i : d;
}

then the type of 'deduceme' would already be covered by the standard.

I think it would be possible to base the rules on 5.16 with extensions to
cater for multiple return expressions.

Roger Orr
--
MVP in C++ at www.brainbench.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: philippe_mori@hotmail.com ("Philippe Mori")
Date: Fri, 17 Jan 2003 19:08:25 +0000 (UTC)
Raw View
I would like to be able to let the compiler deduce the return type of a
function without needing to declare it.

template <typename T = typeof(return)> T f(int i, double d) { return i +
d; }

That is, the return type would be deduced from the return statement. I don't
know what would be the best syntax for that but it could be really usefull
to avoid having to explicitly declare the return type.


I would also like to have the typeof operator that would give the type of an
expression.


And I would like for automatic type deduction for variable declaration:

    typename T t = g();

where T would be a type and t the variable. T could eventually be made
optional if we do not need it afterward (and it does not complicated the
parsing too much).

Also, I would like to be able to let the compiler deduce template argument
of a class when it is possible:

    int *p = new int;
    std::auto_ptr< typename > x = p;

    int *p2= new int;
    std::auto_ptr< typename T > x2(p2);

Here we would have an auto_ptr<int>. Note that only appropriate constructors
would be used for deduction. In the examples above, deduction would be
possible.

I not sure of the best syntax. Using typename seems adequate and would
allows to declare a type name that could be used. Maybe using ... would be
more adequate as it would also works for any number of arguments. In any
case, optional argument needs not to be specified.





---
[ 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: allan_w@my-dejanews.com (Allan W)
Date: Sat, 18 Jan 2003 02:25:32 +0000 (UTC)
Raw View
philippe_mori@hotmail.com ("Philippe Mori") wrote
> I would like to be able to let the compiler deduce the return type of a
> function without needing to declare it.
>
> template <typename T = typeof(return)> T f(int i, double d) { return i +
> d; }
>
> That is, the return type would be deduced from the return statement. I don't
> know what would be the best syntax for that but it could be really usefull
> to avoid having to explicitly declare the return type.

    int foo1(int i, double d) {
        if (i) return i; // Returns int
        else   return d; // Returns double converted to int
    }
    double foo2(int i, double d) {
        if (i) return i; // Returns int converted to double
        else   return d; // Returns double
    }
    deduceme foo3(int i, double d) {
        if (i) return i; // Does it return an int?
        else   return d; // Or does it return a double?
    }


> I would also like to have the typeof operator that would give the type of an
> expression.
>
> And I would like for automatic type deduction for variable declaration:
>
>     typename T t = g();
>
> where T would be a type and t the variable. T could eventually be made
> optional if we do not need it afterward (and it does not complicated the
> parsing too much).

A lot of people would like that. So would I. But I think we should quit
calling it the "typeof operator".
    std::cout << typeof(5);
Should this print "int"? What we really want is syntax that lets us
deduce the type of a variable by it's initialization. Perhaps by
overloading the keyword typename:
    typename t = 5; // t is an int, because 5 is an int literal

Similarly for other places where a type name is used:
    t = static_cast<typename t>(5.0); // Casts 5.0 to an int

If there's some reason why you specifically want "typeof operator,"
please explain.

---
[ 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                       ]