This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Failing test when run as C++11


2014-05-11 19:25 GMT+02:00 Jonathan Wakely <jwakely@redhat.com>:
> On 11/05/14 16:34 +0200, Daniel Krügler wrote:
>> I cannot deduce the full consequences of this change from here, but
>> I'm reading 23.3.6.3 p13 that the following example should be
>> well-formed (C is MoveInsertable and DefaultInsertable):
>>
>> #include <vector>
>>
>> struct C {
>> C() { }
>> C(C&) { }
>> C(C&&){}
>> };
>>
>> int main() {
>> std::vector<C> v(1);
>> v.resize(v.capacity()+1);
>> }
>>
>> Would this still hold under that change?
>
> Yes, that is still well-formed.

Good!

> vector copies when:
>  !is_nothrow_move_constructible<C> && is_copy_constructible<C>
> otherwise it moves.
>
> For your example we do not copy, because is_copy_constructible<C> is
> false (so we use the potentially throwing move and only offer the
> basic exception-safety guarantee).

Makes sense to me.

> My patch changes the behaviour for cases where the
> !is_nothrow_move_constructible<C> && is_copy_constructible<C>
> condition says we should use the copy constructor, but then we
> actually use a different constructor, because we don't do the copying
> with the same argument type as is_copy_constructible<C> checks.
> (We copy from non-const lvalues, but is_copy_constructible is
> equivalent to is_constructible<C, const C&> so tests whether you can
> copy from const lvalues).
>
> So this actually reveals a latent bug: we use the traits to decide
> whether we want to use C(const C&) or C(C&&) but then we actually end
> up using C(C&) or C(C&&), so this fails to compile:
>
> #include <vector>
>
> struct C {
> C() { }
> C(const C&) { }
> C(C&&){}
>
> template<typename T> C(T&) = delete;
>
> };
>
> int main() {
> std::vector<C> v(1);
> v.resize(v.capacity()+1);
> }
>
> With my patch to stl_iterator.h that compiles.
> I'm not sure if it's required to compile or not. I don't think the
> standard is clear what should happen here. I'd be interested to know
> what other implementations do.

My interpretation of the wording is here the same as for my previous
example: It should work, because C is MoveInsertable and
DefaultInsertable.

Both clang and Visual Studio 2012 accept the code.

- Daniel


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]