<html>
<head>
<base href="http://llvm.org/bugs/" />
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW --- - std::vector (and other containers) broken for non primitive objects that happen to have the size of an unsigned char"
href="http://llvm.org/bugs/show_bug.cgi?id=16764">16764</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>std::vector (and other containers) broken for non primitive objects that happen to have the size of an unsigned char
</td>
</tr>
<tr>
<th>Product</th>
<td>libc++
</td>
</tr>
<tr>
<th>Version</th>
<td>unspecified
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Linux
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>normal
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>All Bugs
</td>
</tr>
<tr>
<th>Assignee</th>
<td>hhinnant@apple.com
</td>
</tr>
<tr>
<th>Reporter</th>
<td>tamiko+LLVM@kyomu.43-1.org
</td>
</tr>
<tr>
<th>CC</th>
<td>llvmbugs@cs.uiuc.edu
</td>
</tr>
<tr>
<th>Classification</th>
<td>Unclassified
</td>
</tr></table>
<p>
<div>
<pre>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?</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>