std::fill()-ing a volatile array (via pointers) : c++
This is a discussion on std::fill()-ing a volatile array (via pointers) within the c++ forums in Programming Languages category; Hi, I have the following function template template< typename T, std::size_t n > void secure_fill( volatile T ( &arr )[ n ], const T & value = T() ) { for( std::size_t i( 0 ); i < n; ++i ) { arr[ i ] = value; } } which I use where I want to be sure that the referenced array is always written to and the compiler doesn't optimize away the call "just because" the array itself isn't touched thereafter. Now, if I want to avoid the hand-coded loop and take advantage of the standard library (debug mode and ...
| c++ comp.lang.c++ usenet archive |
![]() |
| | LinkBack | Thread Tools |
|
#1
| |||
| |||
| I have the following function template template< typename T, std::size_t n > void secure_fill( volatile T ( &arr )[ n ], const T & value = T() ) { for( std::size_t i( 0 ); i < n; ++i ) { arr[ i ] = value; } } which I use where I want to be sure that the referenced array is always written to and the compiler doesn't optimize away the call "just because" the array itself isn't touched thereafter. Now, if I want to avoid the hand-coded loop and take advantage of the standard library (debug mode and anything else it might provide) I change it to: template< typename T, std::size_t n > void secure_fill( volatile T ( &arr )[ n ], const T & value = T() ) { std::fill( arr, arr + n, value ); } Is this guaranteed to work? I'm inclined to think that the answer is "yes", because I pass regular pointers as iterators and std::iterator_traits< T * >::value_type is T, which in my case is volatile-qualified. Other opinions? The standard says: [std::fill] assigns value through all the iterators in the range [first ,last ). so I guess it all depends on how "assign through an iterator" in interpreted and whether it provides for anything else than *iter = value or the obvious variants "*iter++ = value", etc. -- Gennaro Prota | <https://sourceforge.net/projects/breeze/> Do you need expertise in C++? I'm available. |
|
#2
| |||
| |||
| On Jul 17, 1:36 pm, Gennaro Prota <inva...@yahoo.com> wrote: > Hi, > > I have the following function template > > template< typename T, std::size_t n > > void > secure_fill( volatile T ( &arr )[ n ], const T & value = T() ) > { > for( std::size_t i( 0 ); i < n; ++i ) { > arr[ i ] = value; > } > } > > which I use where I want to be sure that the referenced array is > always written to and the compiler doesn't optimize away the call > "just because" the array itself isn't touched thereafter. > > Now, if I want to avoid the hand-coded loop and take advantage of the > standard library (debug mode and anything else it might provide) I > change it to: > > template< typename T, std::size_t n > > void > secure_fill( volatile T ( &arr )[ n ], const T & value = T() ) > { > std::fill( arr, arr + n, value ); > } > > Is this guaranteed to work? I'm inclined to think that the answer is > "yes", because I pass regular pointers as iterators and > std::iterator_traits< T * >::value_type is T, which in my case is > volatile-qualified. > > Other opinions? The standard says: > > [std::fill] assigns value through all the iterators in the > range [first ,last ). > > so I guess it all depends on how "assign through an iterator" in > interpreted and whether it provides for anything else than > > *iter = value > > or the obvious variants "*iter++ = value", etc. > > -- > Gennaro Prota | <https://sourceforge.net/projects/breeze/> > Do you need expertise in C++? I'm available. The fill will work fine. |


