[LLVMbugs] [Bug 16764] New: std::vector (and other containers) broken for non primitive objects that happen to have the size of an unsigned char

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Wed Jul 31 15:15:22 PDT 2013


http://llvm.org/bugs/show_bug.cgi?id=16764

            Bug ID: 16764
           Summary: std::vector (and other containers) broken for non
                    primitive objects that happen to have the size of an
                    unsigned char
           Product: libc++
           Version: unspecified
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: All Bugs
          Assignee: hhinnant at apple.com
          Reporter: tamiko+LLVM at kyomu.43-1.org
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

The following does not compile with libc++:

#include<vector>

struct Storage
{
  union
  {
    unsigned char a;
    unsigned char b;
  };
};

int main()
{
  std::vector<Storage> foo;
  foo.insert(foo.end(), 5, Storage());
}



Resulting in the following error:

In file included from test.cc:1:
In file included from /usr/include/c++/v1/vector:261:
In file included from /usr/include/c++/v1/__bit_reference:15:
/usr/include/c++/v1/algorithm:2003:32: error: cannot convert 'const Storage' to
'unsigned char' without a conversion operator
        _VSTD::memset(__first, (unsigned char)__value_, (size_t)(__n));
                               ^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/v1/algorithm:2012:18: note: in instantiation of function
template specialization 'std::__1::__fill_n<Storage *, unsigned long, Storage>'
requested here
   return _VSTD::__fill_n(__first, __n, __value_, integral_constant<bool,
                 ^
/usr/include/c++/v1/vector:1768:24: note: in instantiation of function template
specialization 'std::__1::fill_n<Storage *, unsigned long, Storage>' requested
here
                _VSTD::fill_n(__p, __n, *__xr);
                       ^
test.cc:15:7: note: in instantiation of member function
'std::__1::vector<Storage, std::__1::allocator<Storage> >::insert' requested
here
  foo.insert(foo.end(), 5, Storage());
      ^
1 error generated.



This is due the following check in the algorithm header for memset
acceleration:

/usr/include/c++/v1/algorithm:
2007 template <class _OutputIterator, class _Size, class _Tp>
2008 inline _LIBCPP_INLINE_VISIBILITY
2009 _OutputIterator
2010 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
2011 {
2012    return _VSTD::__fill_n(__first, __n, __value_, integral_constant<bool,
2013                                              
is_pointer<_OutputIterator>::value &&
2014                                              
is_trivially_assignable<typename remove_pointer<_OutputIterator>::type,
_Tp>::value &&
2015                                               sizeof(typename
remove_pointer<_OutputIterator>::type) == 1>());
2016 }



The assumption that a data type with (a) primitive pointer, (b) is trivially
assignable, (c) has the size of an unsigned char doesn't make it automatically
c-style casteable to unsigned char as happens in

1997 template <class _OutputIterator, class _Size, class _Tp>
1998 inline _LIBCPP_INLINE_VISIBILITY
1999 _OutputIterator
2000 __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_,
true_type)
2001 {
2002     if (__n > 0)
2003         _VSTD::memset(__first, (unsigned char)__value_, (size_t)(__n));
2004     return __first + __n;
2005 }


Is a reinterpret_cast the correct choice at this place?

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20130731/99a873d8/attachment.html>


More information about the llvm-bugs mailing list