Topic: typeof keyword
Author: eminhanif@googlemail.com
Date: Tue, 24 Mar 2009 18:06:30 CST Raw View
hi,
i was wondering if a typeof keyword is going to be defined in the new
standard?
i, personnally, would find it very helpful.
--
[ 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: Pavel Minaev <int19h@gmail.com>
Date: Wed, 25 Mar 2009 18:19:23 CST Raw View
On Mar 24, 5:06 pm, eminha...@googlemail.com wrote:
> i was wondering if a typeof keyword is going to be defined in the new
> standard?
Yes, it will be, except that it's called decltype.
In general, you can see the new features in the upcoming standard by
looking at the most current draft. The most recent one is available
online here:
http://www.open-std.org/JTC1/SC22/WG21/
(after opening the page, click on the "Current C++ Standard Working
Paper" link)
--
[ 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: sdt <sjdutoit@gmail.com>
Date: Wed, 25 Mar 2009 18:18:29 CST Raw View
On Mar 24, 8:06 pm, eminha...@googlemail.com wrote:
> hi,
> i was wondering if a typeof keyword is going to be defined in the new
> standard?
> i, personnally, would find it very helpful.
Look for "decltype" in the current draft.
Stefanus
--
[ 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: litb <Schaub-Johannes@web.de>
Date: Wed, 25 Mar 2009 18:18:55 CST Raw View
On 25 Mrz., 01:06, eminha...@googlemail.com wrote:
> hi,
> i was wondering if a typeof keyword is going to be defined in the new
> standard?
> i, personnally, would find it very helpful.
>
Yeah, it's called decltype. It can give you the declared type of a
variable and type of expressions. Search in the working draft (
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2857.pdf )
for decltype. Useful for example in this case:
template<typename A, typename B> auto f(A const& a, B const& b) ->
decltype(a + b) { return a + b; }
Where we get the right return type without "clever" template trickery
and with simple code.
--
[ 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: "Joe Smith" <unknown_kev_cat@hotmail.com>
Date: Wed, 25 Mar 2009 18:28:44 CST Raw View
<eminhanif@googlemail.com> wrote:
> hi,
> i was wondering if a typeof keyword is going to be defined in the new
> standard?
> i, personnally, would find it very helpful.
>
If what you are wanting is something like the following, you are in luck:
int main()
{
int i;
typeof(i) j;
}
(i.e. the typeof() operator acts as the type of the expression so it can be
used to define new variables of that type)
What you are looking for is decltype(), which works exactly like you would
expect ("decltype(i) j;"). It is new in C++0x, but already available in the
latest versions of some compilers. (for example it is present in the latest
Comeau C++ beta, as well as the latest gcc).
--
[ 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: Olivier <olivier.grant@gmail.com>
Date: Thu, 26 Mar 2009 21:45:58 CST Raw View
Why wasn't the wording "typeof" kept instead of "decltype" ?
--
[ 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: Giovanni Deretta <gpderetta@gmail.com>
Date: Fri, 27 Mar 2009 15:40:24 CST Raw View
On Mar 27, 4:45 am, Olivier <olivier.gr...@gmail.com> wrote:
> Why wasn't the wording "typeof" kept instead of "decltype" ?
>
Decltype has slightly different semantics than typeof as implemented
by some compilers (i.e. typeof(x) is more or less equivalent to
remove_reference<decltype(x)>::type) , so the committee changed the
name not to clash with those existing extensions.
Also the new standard has 'auto':
auto x = /expression/;
will be equivalent to:
remove_reference<decltype(/expression/)>::type x = /expression/;
While decltype is mainly useful for generic library writers, I think
'auto' will have a much more widespread usage.
--
gpd
--
[ 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: wasti.redl@gmx.net
Date: Fri, 27 Mar 2009 18:00:35 CST Raw View
On Mar 27, 4:45 am, Olivier <olivier.gr...@gmail.com> wrote:
> Why wasn't the wording "typeof" kept instead of "decltype" ?
>
Because decltype has slightly different semantics than the existing
__typeof and __typeof__ extensions of various compilers, and the
committee wanted to avoid confusion.
--
[ 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: "Joe Smith" <unknown_kev_cat@hotmail.com>
Date: Sat, 28 Mar 2009 12:43:37 CST Raw View
"Olivier" wrote:
> Why wasn't the wording "typeof" kept instead of "decltype" ?
Because the typeof() extention is some compilers is ever so slightly
different than the decltype() feature.
Here is how the typeof operator works in some compilers:
int a;
int &b=a;
typeof(a) c; //This declares an int named c.
typeof(b) d; //This declares an int named d.
But with decltype:
int a;
int &b=a;
decltype(a) c; //This declares an int named c.
decltype(b) d; //This declares an int& named d.
--
[ 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: google@dalvander.com
Date: Sun, 29 Mar 2009 15:51:24 CST Raw View
On Mar 27, 11:40 pm, Giovanni Deretta <gpdere...@gmail.com> wrote:
> auto x = /expression/;
>
> will be equivalent to:
>
> remove_reference<decltype(/expression/)>::type x = /expression/;
Are you sure?
Let's say I have a class which is heavy to copy assign and copy
construct: HeavyObject. And a function which returns a HeavyObject by
const-reference:
const HeavyObject& getHeavy(int howHeavy);
And then do the following:
auto heavy = getHeavy(42);
The copy constructor will be called?
Do I need to use the following to prevent the copy constructor to be
called?
const auto& heavy = getHeavy(42);
Regards,
Anders Dalvander
--
[ 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: Pavel Minaev <int19h@gmail.com>
Date: Tue, 31 Mar 2009 13:08:31 CST Raw View
On Mar 29, 2:51 pm, goo...@dalvander.com wrote:
> Let's say I have a class which is heavy to copy assign and copy
> construct: HeavyObject. And a function which returns a HeavyObject by
> const-reference:
> const HeavyObject& getHeavy(int howHeavy);
>
> And then do the following:
> auto heavy = getHeavy(42);
>
> The copy constructor will be called?
Yes.
> Do I need to use the following to prevent the copy constructor to be
> called?
> const auto& heavy = getHeavy(42);
Yes.
The very simple rule of thumb is that "auto" works the same way as
template argument deduction works (in fact, the draft standard says as
much about auto: "Once the type of a declarator-id has been determined
according to 8.3, the type of the declared variable using the
declarator-id is determined from the type of its initializer using the
rules for template argument deduction.").
--
[ 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: Giovanni Deretta <gpderetta@gmail.com>
Date: Tue, 31 Mar 2009 13:08:31 CST Raw View
On Mar 29, 11:51 pm, goo...@dalvander.com wrote:
> On Mar 27, 11:40 pm, Giovanni Deretta <gpdere...@gmail.com> wrote:
>
> > auto x = /expression/;
>
> > will be equivalent to:
>
> > remove_reference<decltype(/expression/)>::type x = /expression/;
>
> Are you sure?
I haven't checked the exact wording, nor I know it there have been
recent changes, but I'm fairly sure that yes, that's the way auto
works.
It pretty much mimics function template argument deduction (and IIRC
it is specified in term of them).
>
> Let's say I have a class which is heavy to copy assign and copy
> construct: HeavyObject. And a function which returns a HeavyObject by
> const-reference:
> const HeavyObject& getHeavy(int howHeavy);
>
> And then do the following:
> auto heavy = getHeavy(42);
>
> The copy constructor will be called?
Yes.
>
> Do I need to use the following to prevent the copy constructor to be
> called?
> const auto& heavy = getHeavy(42);
>
Yes again.
Or make getHeavy return by value and rely on RVO. Or make the heavy
object movable.
--
gpd
--
[ 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 ]