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. 1 2728
On Jul 17, 1:36*pm, Gennaro Prota <inva...@yahoo.comwrote:
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: ma740988 |
last post by:
Consider:
# include <iostream>
using std::cout;
using std::cin;
using std::endl;
# include <list>
using std::list;
|
by: Saeed Amrollahi |
last post by:
Dear All C++ Programmers
Hello
I am Saeed Amrollahi. I am a software engineer in
Tehran Sewerage Company. I try to use std::map and map::find member
function. I
use Visual Studio .NET. my...
|
by: Steven T. Hatton |
last post by:
I know of a least one person who believes std::ifstream::read() and
std::ofstream::write() are "mistakes". They seem to do the job I want
done. What's wrong with them. This is the code I...
|
by: ma740988 |
last post by:
The hierachy for standard exception lists:
bad_alloc, bad_cast, bad_typeid, logic_error, ios_base::failure,
runtime_error and bad_exception
runtime_error and logic_error has subsets.
The...
|
by: Geoffrey S. Knauth |
last post by:
It's been a while since I programmed in C++, and the language sure has
changed. Usually I can figure out why something no longer compiles, but
this time I'm stumped. A friend has a problem he...
|
by: ma740988 |
last post by:
I've allocated 4K memory and I'd like to use std::fill to fill each 1K
with a different value. Note: I could easily use a vector/deque but
I'm interested in a C style array.
int main()
{
int...
|
by: ma740988 |
last post by:
Trying to get more acclimated with the use of function objects. As
part of my test, consider:
# include <vector>
# include <iostream>
# include <algorithm>
#include <stdexcept>
#include...
|
by: ma740988 |
last post by:
I've been perusing for some time now - I suppose some of the algorithms
in Josuttis. 'Today', I do alot of funny math on data resident within
memory. As part of this. I find myself using...
|
by: eb |
last post by:
I have this working code :
foo.h
/* nothing */
foo.cpp
...
std::vector<std::vector<T * my_T(board_size,board_size) ;
for (i=0; i<board_size; i++)
|
by: JDT |
last post by:
Hi,
Can someone show me how to set any integer (or float) in an std::vector
as zero in a way other than using a for loop? Can we apply memset() or
ZeroMemory() to the vector? Thanks for your...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |