|
Boost : |
Subject: Re: [boost] [date_time] default constructor for period?
From: Chris Glover (c.d.glover_at_[hidden])
Date: 2015-05-01 14:43:45
>
> Chris,
>
> Yes, you are right! It wasn't std::deque I had the problem with. In fact,
> it was std::map; specifically operator[](). Technically, I could work
> around that by using insert() and testing the return value and, if
> necessary, calling operator[](). But that's neither very elegant nor
> maintainable, in my opinion. Especially when I've yet to see a downside of
> adding a default constructor.
>
> Furthermore, I have other other container code that depends on default
> constructors. Sure, I could workaround period's lack of default
> constructor, but wheres the advantage in that?
>
> SB
>
>
>
Hi Scott,
Yes, using operator[] requires the object to be default constructible
Working around that requires doing some sort of find first. Something like
this (or possibly more efficiently with lower_bound if your items are
equality comparable).
auto iter = map.find(key);
if(iter == map.end())
map.insert(make_pair(key, item));
else
*iter = item;
At any rate, I am not sure why the original authors decided that this type
should not be default constructible. I think most value-like types should
be, but not always, so it's not clear if this is an oversight or
intentional. Someone with more knowledge of the library might know. I was
more interested in that specific deque problem you were having.
-- chris
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk