[cfe-dev] Fwd: [libc++] Is libc++ compatible with previous c++ language standard implementation

alan snape via cfe-dev cfe-dev at lists.llvm.org
Wed Mar 7 20:25:13 PST 2018


OK, I got your meaning. Let's summarize the interfaces declared on
http://en.cppreference.com/w/cpp/container/vector/vector and then go over
the implementation of libc++ version.

On the site http://en.cppreference.com/w/cpp/container/vector/vector, the
given interfaces are:

> (2)
> explicit vector( size_type count,
>                  const T& value = T(),
>                  const Allocator& alloc = Allocator());
> (until C++11)
>          vector( size_type count,
>                  const T& value,
>                  const Allocator& alloc = Allocator());
> (since C++11)
> (3)
> explicit vector( size_type count );
> (since C++11, until C++14)
> explicit vector( size_type count, const Allocator& alloc = Allocator() );
> (since C++14)

If you use `vector(1)`, format (2-1) will be used until c++11, format (3-1)
will be used since c++11 until c++14 and format (3-2) will be used since
c++14.
If you use `vector(1, T())`, format (2-1) will be used until c++11, format
(2-2) will be used since c++11.
If you use `vector(1, T(), Allocator())`, format (2-1) will be used until
c++11, format (2-2) will be used since c++11.
And you can use `vector(1, Allocator())` until c++14, where format (3-2)
will be used.
And the above interfaces are right the same with the code provided by gcc
(just as what you pasted in the previous mail).

Then, let's have a look at the libc++ version:

> // libc++/include/vector:491~496:
> https://github.com/llvm-mirror/libcxx/blob/master/include/vector#L491



    explicit vector(size_type __n);  // (1)
> #if _LIBCPP_STD_VER > 11
>     explicit vector(size_type __n, const allocator_type& __a);  // (2)
> #endif
>     vector(size_type __n, const_reference __x);  // (3)
>     vector(size_type __n, const_reference __x, const allocator_type&
> __a);  // (4)

If you use `vector(1)`, format (1) will be used.
If you use `vector(1, T())`, format (3) will be used.
If you use `vector(1, T(), Allocator())`, format (4) will be used.
And you can use `vector(1, Allocator())` until c++14, where format (2) will
be used.
All the interfaces required are implemented, and clearer than the gcc
version.

Did I managed to answer your question?

And go on with the test case you provided above, the differences between
the two versions of header files make the different results. In gcc, format
(2-1) will be used until c++11, which may contain value copy during
construction, but the format (3-1) and format (3-2), which are used in
c++11 and since c++14 respectively, do not have the value copy; while in
clang, only format (1) will be used, and the behavior cannot be different.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180308/a13c22cd/attachment.html>


More information about the cfe-dev mailing list