Question

I have some (say, dynamically generated) code and STL container with POD data (say, std::vector< T > of static_assert(std::is_floating_point< T >::value, "!");) which accessed from that code.

Apparently I need to declare the data with the volatile qualifier. But in such case gcc emits an errors:

c:\mingw\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\ext\new_allocator.h:110: ошибка: invalid conversion from 'volatile void*' to 'void*' [-fpermissive]
       { ::operator delete(__p); }

c:\mingw\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\bits\stl_construct.h:75: ошибка: invalid static_cast from type 'volatile double*' to type 'void*'
     { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }

c:\mingw\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\bits\stl_algobase.h:372: error: invalid conversion from 'volatile void*' to 'void*' [-fpermissive]
      __builtin_memmove(__result, __first, sizeof(_Tp) * _Num);
                                                         ^
c:\mingw\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\bits\stl_algobase.h:372: error: invalid conversion from 'const volatile void*' to 'const void*' [-fpermissive]

Is this the only solution, that I should provide my own allocator with proper correction of pointer conversions?

Was it helpful?

Solution

A vector of volatile objects cannot work with the standard allocator. There is a GCC bug open on this, and it looks like a defect in the standard, not in GCC.

You have to provide your own allocator.

OTHER TIPS

If you have to cast from volatile T * to T* you need to use const_cast (surprise, surprise!). Further cast to void * will require static_cast or reinterpret_cast.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top