Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++.moderated > #1388
| Message-ID | <iskrrf$mbf$1@dont-email.me> (permalink) |
|---|---|
| Newsgroups | comp.lang.c++.moderated |
| From | Daniel Krügler <daniel.kruegler@googlemail.com> |
| Subject | Re: codecvt problem |
| Organization | A noiseless patient Spider |
| References | <3fe789b8-a729-4623-9e7e-301984e6c053@z13g2000yqg.googlegroups.com> <4DEBC922.2060901@sh.cvut.cz> <isig0g$rj7$1@dont-email.me> <isj57l$cfl$1@dont-email.me> <nbv0c8-7vq.ln1@satorlaser.homedns.org> |
| Date | 2011-06-07 10:31 -0600 |
On 2011-06-07 11:07, Ulrich Eckhardt wrote:
> Daniel Krügler wrote:
>> Am 06.06.2011 20:50, schrieb Jeff Flinn:
>>> Vaclav Haisman wrote:
>>>> Rafał Moniuszko wrote, On 5.6.2011 15:25:
>>>> [...]
>>>> Add #include<cstring> and do
>>>> std::memset (&mystate, 0, sizeof(mystate));:
>>>
>>> Why not mbstate_t mystate = {0};
>>
>> Or rely on aggregate-initialization with an empty initializer:
>>
>> mbstate_t mystate = {};
>
> Just wondering is that the same or are there subtle differences?
It is not guaranteed to be portable in C++03, because empty-list
initialization is only specified for aggregates, not for other types
(like scalar types), and mbstate_t is just specified to be a non-array
object type, therefore it may be an aggregate or not. It is portable in
C++0x because of the new concept of "list-initialization" and because of
a subtle change of a zero-initialization border case, see below.
In response to your actual question I would say that it should have the
same effects, assuming that mbstate_t is a C compatible object type (and
I don't think that anything else makes sense). In this case {} has the
effect of value-initializing all components of an aggregate and it has
the effect of value-initializing an object of scalar type.
Initializing an aggregate or a scalar with {0} is equivalent to the
effects that value-initialization has for such types, because in the end
we have a zero-initialization of all scalar sub-parts and this has the
effects of "set to the value of 0 (zero) converted to T;". This is near
to identical to memset, except for (to the best of my knowledge)
a) padding bytes before C++0x, but the FDIS has approved the core issue
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#694
fixing this subtle difference and
b) pointer types, because memset does not guarantee to have the same
effects as zero-initializing. In the latter case it is guaranteed to
have a null pointer value, but memset applied to a pointer value may be
something different.
I would say that especially the second difference is more a pro argument
to rely on value-initialization instead of using memset.
> Also, what
> about this C++ solution:
>
> mbstate_t mystate = mbstate_t();
I would say that this is the best compromise if you want to support both
C++03 and C++0x code targets. It has the effect of value-initialization,
so it is equivalent to {} in C++0x and {0} in C++03 (for the subset of
types that I mentioned one or two special cases mentioned above.
HTH & Greetings from Bremen,
Daniel Krügler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to comp.lang.c++.moderated | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
codecvt problem Rafał Moniuszko <rafal.lists@gmail.com> - 2011-06-05 07:25 -0600
Re: codecvt problem Vaclav Haisman <v.haisman@sh.cvut.cz> - 2011-06-05 21:41 -0600
Re: codecvt problem Jeff Flinn <TriumphSprint2000@hotmail.com> - 2011-06-06 12:50 -0600
Re: codecvt problem Vaclav Haisman <v.haisman@sh.cvut.cz> - 2011-06-06 15:31 -0600
Re: codecvt problem Martin Bonner <martinfrompi@yahoo.co.uk> - 2011-06-08 02:06 -0600
Re: codecvt problem Vaclav Haisman <v.haisman@sh.cvut.cz> - 2011-06-08 17:24 -0600
Re: codecvt problem Seungbeom Kim <musiphil@bawi.org> - 2011-06-09 07:02 -0600
Re: codecvt problem Vaclav Haisman <v.haisman@sh.cvut.cz> - 2011-06-16 17:04 -0600
Re: codecvt problem Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2011-06-17 06:42 -0600
Re: codecvt problem Seungbeom Kim <musiphil@bawi.org> - 2011-06-18 07:38 -0600
Re: codecvt problem Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2011-06-20 13:16 -0600
Re: codecvt problem Seungbeom Kim <musiphil@bawi.org> - 2011-06-23 14:42 -0600
Re: codecvt problem Seungbeom Kim <musiphil@bawi.org> - 2011-06-09 07:01 -0600
Re: codecvt problem Daniel Krügler <daniel.kruegler@googlemail.com> - 2011-06-06 15:31 -0600
Re: codecvt problem Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2011-06-07 03:07 -0600
Re: codecvt problem Daniel Krügler <daniel.kruegler@googlemail.com> - 2011-06-07 10:31 -0600
Re: codecvt problem Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2011-06-06 12:47 -0600
csiph-web