#149 std::copy on volatile

Francois Dumont

Extracted from Help forum:

Using STLport 5.1.13 with code like this:

#include <algorithm>

int main()
const std::size_t size = 10;
int a[ size ];
volatile int va[ size ];
std::copy( a, a + size, va );

I get an error because of the volatile when calling the STLport internal function __copy_trivial(), which is the point when the parameters are no longer templatized and are converted to [const] void* at stl/_algobase.h:257. Here's the code for __copy_trivial():

inline void*
__copy_trivial(const void* __first, const void* __last, void* __result) {
size_t __n = (const char*)__last - (const char*)__first;
return __n ? (void *)((char*)memmove(__result, __first, __n) + __n) : __result;

which is called through these functions (all from the same file):

template <class _InputIter, class _OutputIter>
inline _OutputIter __copy_ptrs(_InputIter __first, _InputIter __last, _OutputIter __result,
const __true_type& /*IsOKToMemCpy*/) {
// we know they all pointers, so this cast is OK
// return (_OutputIter)__copy_trivial(&(*__first), &(*__last), &(*__result));
return (_OutputIter)__copy_trivial(__first, __last, __result);

template <class _InputIter, class _OutputIter>
inline _OutputIter __copy_aux(_InputIter __first, _InputIter __last, _OutputIter __result,
const __true_type& /*BothPtrType*/) {
return __copy_ptrs(__first, __last, __result,
_UseTrivialCopy(_STLP_VALUE_TYPE(__first, _InputIter),
_STLP_VALUE_TYPE(__result, _OutputIter))._Answer());

template <class _InputIter, class _OutputIter>
inline _OutputIter copy(_InputIter __first, _InputIter __last, _OutputIter __result) {
_STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
return _STLP_PRIV __copy_aux(__first, __last, __result, _BothPtrType< _InputIter, _OutputIter>::_Answer());

Should there be a set of volatile overloads for __copy_trivial? (Incidentally, I didn't get this error with STLport 4.6.2.)



  • Logged In: YES
    Originator: YES

    Fix in trunk. Now volatile qualifier disable any attempt of optimization through use of memcpy/memmove.

    • status: open --> closed
    • status: closed --> closed-fixed