Topic: default and copy construct vs. value-init


Author: "Balog Pal" <pasa@lib.hu>
Date: Mon, 10 Aug 2009 12:44:12 CST
Raw View
The N+1th time I see on clc that someone stumbled on the 'can be read as
declaration' landmine we nrture in C/C++.   Within a template we may need a
properly inited object of the templated type T.

T t;   //(1)

Does not do the trick, unfortunately, we woulf need

T t();

but that is a declaration.
The suggested way was to write

T t = T();

with note that (the problem in the referred post)

T t( T() );

does not work either being declaration again.

This is bad enough, but then problem I was thinking is about type
requirements.  Suppose the original plan on the templated type was
default-constructible, as the code actually needs such an object.  Being
unabble to write the clean (1) form forces workaround with copy
construction.  The compiler certainly will not create extra copies, but it
still creates requirement of copy-constructible (maybe move-constructible).
The implementation will (IUC) for noncopyable types.

My question is whether we have another workaround to the problem without
this trait?  And whether C++0x does something about the root of the problem,
and any of the new forms allow to use empty direct-init normally, or any
tool that prevents the declaration-first thinking.





--
[ 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: Jerry Coffin <jerryvcoffin@yahoo.com>
Date: Tue, 11 Aug 2009 14:08:55 CST
Raw View
In article <h5p61f$2cct$1@news.ett.com.ua>, pasa@lib.hu says...
>
> The N+1th time I see on clc that someone stumbled on the 'can be read as
> declaration' landmine we nrture in C/C++.   Within a template we may need a
> properly inited object of the templated type T.
>
> T t;   //(1)
>
> Does not do the trick, unfortunately, we woulf need
>
> T t();
>
> but that is a declaration.

[ ... ]

> My question is whether we have another workaround to the problem
> without this trait?  And whether C++0x does something about the
> root of the problem, and any of the new forms allow to use empty
> direct-init normally, or any tool that prevents the declaration-
> first thinking.

Yes -- C++ 0x provides braced init lists, which can be empty, so you
can use:

T t{};

--
   Later,
   Jerry.

[ 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: McNepp <gneppert@web.de>
Date: Tue, 11 Aug 2009 14:12:30 CST
Raw View
On 10 Aug., 18:44, "Balog Pal" <p...@lib.hu> wrote:
> The N+1th time I see on clc that someone stumbled on the 'can be read as
> declaration' landmine we nrture in C/C++.   Within a template we may need a
> properly inited object of the templated type T.
>
> T t;   //(1)
>
> Does not do the trick, unfortunately, we woulf need

Please explain why this "does not do the trick". AFAIK, this does
create a default-constructed T-object.
The only catch is that it doesn't initialize built-in types, but it's
hard to see how this would become a problem, since the function that
declares this T-object will always assign something to it as the next
step, right?

Please note that the problem can only arise with local variables, not
with class members!

And a local T-object is next to useless if it can't escpace the
function sope, which it only can if it has a copy-constructor, in
which case you could easily write a code path such as "return T();"...










--
[ 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: "Balog Pal" <pasa@lib.hu>
Date: Wed, 12 Aug 2009 01:53:18 CST
Raw View
"McNepp" <gneppert@web.de>
>> The N+1th time I see on clc that someone stumbled on the 'can be read as
>> declaration' landmine we nrture in C/C++.   Within a template we may need
>> a
>> properly inited object of the templated type T.
>>
>> T t;   //(1)
>>
>> Does not do the trick, unfortunately, we would need
>
> Please explain why this "does not do the trick". AFAIK, this does
> create a default-constructed T-object.
> The only catch is that it doesn't initialize built-in types, but it's
> hard to see how this would become a problem, since the function that
> declares this T-object will always assign something to it as the next
> step, right?

Wrong. It can be passed as an object to store in a collection, copied, etc.
In general, a default/value inited object is good for any purpose, and needs
no further assignment to it as a first step.

I'm glad brace-init cures this problem, it also seem pretty versatile...



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