G
Gennaro Prota
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.
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.