473,473 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

std::fill and containers of pointers


I got an error by using std::fill to set an array of pointers to 0.

e.g.:

class XXX;
XXX* v[30];

std::fill(v, v+30, 0); // <-- ERROR -- cant' match template type

I have to either explicitly instantiate std::fill<> or cast 0 to XXX*.
I've been using th latter:

std::fill(v, v+30, static_cast<XXX*>(0));

I understand the issue, that template instantiation processing is
interpreting the 0 as an integer rather than a pointer value (and thus
I have to help it along with the cast). Is there any other workaround
for this? Will the proposed "nullptr" be handled properly in this sort
of case?

Jul 22 '05 #1
3 2918
red floyd wrote:
I got an error by using std::fill to set an array of pointers to 0.

e.g.:

class XXX;
XXX* v[30];

std::fill(v, v+30, 0); // <-- ERROR -- cant' match template type

I have to either explicitly instantiate std::fill<> or cast 0 to XXX*.
I've been using th latter:

std::fill(v, v+30, static_cast<XXX*>(0));
You could use a C-style cast, it basically means the same

std::fill(v, v+30, (XXX*)0);
I understand the issue, that template instantiation processing is
interpreting the 0 as an integer rather than a pointer value (and thus
I have to help it along with the cast). Is there any other workaround
for this?
If there is, I don't know it.
Will the proposed "nullptr" be handled properly in this sort
of case?


It's a good question. The function template std::fill is specified as

template<class FwdIter, class T>
void fill(FwdIter first, FwdIter last, const T& value);

or

template<class OutIter, class Size, class T>
void fill_n(OutIter first, Size n, const T& value);

which would require that T is derived from the third argument in a call,
and that's possible because 'nullptr' has a distinct type. But there is
a catch: binding an r-value to a const reference requires copy semantics
to be defined for the type T, and in the proposal 'decltype(nullptr)' is
such that no additional objects with that type can be created. 8.5.3 is
the relevant section of the Standard.

Perhaps along with 'nullptr' proposal, they will fix (relax) the
requirement that a copy has to be _creatable_ even in the case when no
copy is actually made...

A good topic for comp.std.c++ (but I've not been following that newsgroup
closely).

Victor
Jul 22 '05 #2
Victor Bazarov wrote:
red floyd wrote:
I got an error by using std::fill to set an array of pointers to 0.

e.g.:

class XXX;
XXX* v[30];

std::fill(v, v+30, 0); // <-- ERROR -- cant' match template type

I have to either explicitly instantiate std::fill<> or cast 0 to XXX*.
I've been using th latter:

std::fill(v, v+30, static_cast<XXX*>(0));

You could use a C-style cast, it basically means the same

std::fill(v, v+30, (XXX*)0);
I understand the issue, that template instantiation processing is
interpreting the 0 as an integer rather than a pointer value (and thus
I have to help it along with the cast). Is there any other workaround
for this?

If there is, I don't know it.
> Will the proposed "nullptr" be handled properly in this sort

of case?


Yes. I know better now.
It's a good question. The function template std::fill is specified as

template<class FwdIter, class T>
void fill(FwdIter first, FwdIter last, const T& value);

or

template<class OutIter, class Size, class T>
void fill_n(OutIter first, Size n, const T& value);

which would require that T is derived from the third argument in a call,
and that's possible because 'nullptr' has a distinct type. But there is
a catch: binding an r-value to a const reference requires copy semantics
to be defined for the type T, and in the proposal 'decltype(nullptr)' is
such that no additional objects with that type can be created. 8.5.3 is
the relevant section of the Standard.


I just read the second revision of the proposal (duh!) and they changed
the requirement. Now it allows variables of that type created, so there
will be no problem with copying of 'nullptr' if necessary. That takes
care of the 'const T&' issue, and the answer to your question is "yes".

You can see the second revision of the proposal here:
http://www.open-std.org/jtc1/sc22/wg...2004/n1601.pdf

Victor
Jul 22 '05 #3
Victor Bazarov wrote:
[std::fill, 0, and nullptr discussion redacted]

You can see the second revision of the proposal here:
http://www.open-std.org/jtc1/sc22/wg...2004/n1601.pdf

Victor


Thanks, Victor.

red floyd
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dylan | last post by:
If I reserve some memory for a std container, let's say std::vector<int> myInts; myInts.reserve(100); and then allocate another std:vector to it, like so: std::vector<int> myOtherInts(30,...
4
by: Andy Venikov | last post by:
Is there a way to change standard containers' behavior to expect a 0 when allocation fails (same way you can change operator new's behavior to return null by passing std::nothrow) ? In my...
12
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...
37
by: jortizclaver | last post by:
Hi, I'm about to develop a new framework for my corporative applications and my first decision point is what kind of strings to use: std::string or classical C char*. Performance in my system...
2
by: cpisz | last post by:
I saw that using std::fill was the way to go for setting all elements of an array to some value in one foul swoop. However when I tryed it I am getting an error. Can I only use this for vectors...
1
by: cpisz | last post by:
I am going to make a queue of events to be dispatched to 1 or many event handlers. I know I read that in order to use a std container I must implement a default constructor, a copy constructor, and...
3
by: janzon | last post by:
Hi I have a really weird bug. An object in my program contains a std::stack<T>. When push is called, the program crashes. To eliminate that the error has to do with what is pushed, I create a...
7
by: wangxiaohu | last post by:
I am trying to write a class myVector based on std::vector and add only a sort method. The sort method needs to access the array stored in the base class using iterator. I have following draft...
1
by: Gennaro Prota | last post by:
Hi, I have the following function template template< typename T, std::size_t n > void secure_fill( volatile T ( &arr ), const T & value = T() ) { for( std::size_t i( 0 ); i < n; ++i ) {...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
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,...
0
jinu1996
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...
0
tracyyun
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...
0
agi2029
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,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.