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? 3 2941
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(nullp tr)' 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
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(nullp tr)' 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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, 10);
myInts = myOtherInts;
|
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 environment I can't use exceptions. I can't use
standard allocators either (embedded, you get the picture).
I recompiled STLPort with _no_exceptions, but when my
allocator failed (returned 0) the whole thing crashed.
My guess is that recompiling with...
|
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 const max = 0x1000;
int *ptr_mem = new int ;
int initial(1);
for ( int idx(0); idx < 4; ++idx )
|
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 is quite importante - it's not a realtime
system, but almost - and I concern about std::string performance in
terms of speed. No doubt to use std implementation is a lot easier but
I can't sacrifice speed.
|
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 or can I use it for a regular array?
what I called:
std::fill(m_start_times, m_start_times, 0);
error:
| |
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 an
assignment operator. However, do any of these apply if I only queue up
pointers to events and then release the allocated memory after the
event has been dispatched and processed? And is that a bad idea?
|
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 new local
stack that has nothing to do with any other code. Then I push a 5 and
the program crashes. In detail, i added
stack<doubleanewstack; // Code that has nothing to do with the
rest of the program
|
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 code. However
it never compile. Can anyone take a look and tell me how to fix it?
Thanks!
#include <vector>
#include <iostream>
using namespace std;
|
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 ) {
arr = value;
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |