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

Zeson Wu via cfe-dev cfe-dev at lists.llvm.org
Wed Mar 7 18:38:13 PST 2018


Sorry to not reply to all. So FYI.

---------- Forwarded message ----------
From: Zeson Wu <westion717 at gmail.com>
Date: 2018-03-07 16:26 GMT+08:00
Subject: Re: [cfe-dev] [libc++] Is libc++ compatible with previous c++
language standard implementation
To: Craig Topper <craig.topper at gmail.com>


although calling code shouldn't be able to tell the difference and it
doesn't require any code changes on the user's part, I think the interface
was changed and the semantic is also changed.
The following case will show different behavior. Or it's UB test point?

#include <iostream>
> #include <vector>
>
> struct T
> {
>    bool flag;
>    T() : flag(false) {}
>    T(const T&) : flag(true) {}
> };
>
>
> int main()
> {
>    std::vector<T> test(1);
>    bool is_cpp11 = !test[0].flag;
>
>    std::cout << is_cpp11 << std::endl ;
> }
>

Anyway, I find gcc libstdc++ has a guard to implement different version as
following? But llvm libc++ is not.

#if __cplusplus >= 201103L
> /**
> * @brief Creates a %vector with default constructed elements.
> * @param __n The number of elements to initially create.
> * @param __a An allocator.
> *
> * This constructor fills the %vector with @a __n default
> * constructed elements.
> */
> explicit
> vector(size_type __n, const allocator_type& __a = allocator_type())
> : _Base(__n, __a)
> { _M_default_initialize(__n); }
> /**
> * @brief Creates a %vector with copies of an exemplar element.
> * @param __n The number of elements to initially create.
> * @param __value An element to copy.
> * @param __a An allocator.
> *
> * This constructor fills the %vector with @a __n copies of @a __value.
> */
> vector(size_type __n, const value_type& __value,
> const allocator_type& __a = allocator_type())
> : _Base(__n, __a)
> { _M_fill_initialize(__n, __value); }
> #else
> /**
> * @brief Creates a %vector with copies of an exemplar element.
> * @param __n The number of elements to initially create.
> * @param __value An element to copy.
> * @param __a An allocator.
> *
> * This constructor fills the %vector with @a __n copies of @a __value.
> */
> explicit
> vector(size_type __n, const value_type& __value = value_type(),
> const allocator_type& __a = allocator_type())
> : _Base(__n, __a)
> { _M_fill_initialize(__n, __value); }
> #endif




2018-03-07 15:25 GMT+08:00 Craig Topper <craig.topper at gmail.com>:

> I'm not sure about libc++'s support for pre-C++11, but I think it does
> support earlier standards. The constructor you're asking about isn't a
> great example to look at. The function signature (1) was split into two
> different functions in C++11 rather than using a defaulted argument. But
> the calling code shouldn't be able to tell the difference. It doesn't
> require any code changes on the user's part.
>
> It also looks like even the defaulted argument for alloc is implemented in
> libc++ with different signatures which is different than what the spec
> says. But again user code can't tell.
>
> ~Craig
>
> On Tue, Mar 6, 2018 at 10:38 PM, Zeson Wu via cfe-dev <
> cfe-dev at lists.llvm.org> wrote:
>
>> Hi, All.
>>
>> Does anybody know that whether libc++ is compatible with previous c++
>> language standard implementation or not? For example, I know libc++ is now
>> support c++11, I will show a example.
>>
>> http://en.cppreference.com/w/cpp/container/vector/vector. In the
>> reference, there are two different constructors for vector,
>>
>> explicit vector( size_type count, const T& value = T(), const Allocator&
>> alloc = Allocator()); (until C++11) *(1)*
>> explicit vector( size_type count );(since C++11) (until C++14) *(2)*
>>
>> If I use vector<T> a(1), which one will be chosen with different c++ std
>> command option, such as -std=c++03, -std=c++11? Actually, I can not find
>> the (1) version in libc++ source.
>>
>> In my option, both two functions should be implemented and guarded by
>> related language level macro, but I can not find in libc++ source. So
>> libc++ is not compatible with previous c++ language standard implementation?
>>
>> Thanks.
>>
>> --
>> Zeson
>>
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>>
>>
>


-- 
Zeson



-- 
Zeson
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180308/2fdd3e39/attachment.html>


More information about the cfe-dev mailing list