Question about pointer-to-data members
I have a deeply nested loop, the innermost bit looks a little like this:
for(it(vec.begi n(), end(vec.end()), it!=end;++it){
if (global == 0){
myObj = it->func().secon d->anObj();
} else {
myObj = it->func().first->anObj();
}
myVec.push_back (anObj);
}
where it->func() returns a pair of objects' pointers with 'anObj' functions which return an object
suitable for insertion in myVec.
Now, given that 'global' is known before I enter any loops, I could in principle decide to use the
'first' or 'second' before I ever enter this loop, instead of deciding inside the loop (and
hopefully thus save a few ns of time)... I thought to use a pointer-to-member (the member being
'first' or 'second') but cannot figure out the syntax.
This is what I am trying:
const primaryObj * pair<const primaryObj *, const primaryObj *>::*pFirstOrSe cond;
pFirstOrSecond = global? (&pair<const primaryObj *,const primaryObj *>::first):(pai r<const
primaryObj *,const primaryObj *>::second);
the compiler complains :
.../src/SCTHitsNoiseMon Tool.cxx: In member function `StatusCode
SCTHitsNoiseMon Tool::generalHi stsandNoise()':
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_pair.h:74: error:
object missing in reference to `std::pair<cons t Trk::PrepRawDat a*, const Trk::PrepRawDat a*>::second'
where Trk::PrepRawDat a corresponds to 'primaryObj' in the example above, as if it is looking for the
concrete object rather than the generic pointer-to-member.
can anyone help?
cheers
shaun 9 3725
On 11 Jun, 11:32, shaun roe <shaun....@wana doo.frwrote:
Question about pointer-to-data members
I have a deeply nested loop, the innermost bit looks a little like this:
for(it(vec.begi n(), end(vec.end()), it!=end;++it){
* if (global == 0){
* * myObj = it->func().secon d->anObj();
* } else {
* * myObj = it->func().first->anObj();
* }
* myVec.push_back (anObj);
}
where it->func() returns a pair of objects' pointers with 'anObj' functions which return an object
suitable for insertion in myVec.
Now, given that 'global' is known before I enter any loops, I could in principle decide to use the
'first' or 'second' before I ever enter this loop, instead of deciding inside the loop (and
hopefully thus save a few ns of time)... I thought to use a pointer-to-member (the member being
'first' or 'second') but cannot figure out the syntax.
This is what I am trying:
*const primaryObj * pair<const primaryObj *, const primaryObj *>::*pFirstOrSe cond;
* *pFirstOrSecond = global? (&pair<const primaryObj *,const primaryObj *>::first):(pai r<const
primaryObj *,const primaryObj *>::second);
the compiler complains :
[snip]
can anyone help?
I think I understand what you are trying to do, but I don't think it's
possible, because there is no special "pointer to data-member".
Such a pointer would on most implementations be equivalent to the byte
offset from the beginning of the object's data structure to the data
member, IOW very similar to the "offsetof" macro.
However, you can't (formally) use offsetof on a pair, because it is
not strictly POD (it has a user-declared ctor). It would still
probably work on most implementations .
Other options include writing the loop twice:
if(global == 0)
// entire loop
else
// entire loop
Or rewrite the loop using functors to fetch the object, something like
this:
if(global == 0)
std::transform( vec.begin(), vec.end(), std::back_inser ter(myVec),
get1st());
else
std::transform( vec.begin(), vec.end(), std::back_inser ter(myVec),
get2nd());
Which may or may not be faster than the original loop ;)
DP
On Jun 11, 5:32*am, shaun roe <shaun....@wana doo.frwrote:
Question about pointer-to-data members
I have a deeply nested loop, the innermost bit looks a little like this:
for(it(vec.begi n(), end(vec.end()), it!=end;++it){
* if (global == 0){
* * myObj = it->func().secon d->anObj();
* } else {
* * myObj = it->func().first->anObj();
* }
* myVec.push_back (anObj);
}
can anyone help?
I think you are heading into Knuthian territory here, but, perhaps you
could add:
it->funcFirst() and it->funcSecond()
Then outside the loop create a pointer to one or the other of these
functions (since I presume they have the same signature).
Then you _could_ use this pointer to member function inside the loop.
Joe Cook
shaun roe wrote:
Question about pointer-to-data members
I have a deeply nested loop, the innermost bit looks a little like this:
for(it(vec.begi n(), end(vec.end()), it!=end;++it){
Huh? Could it be that you are trying to do the job of the compilers
optimizing pass?
if (global == 0){
myObj = it->func().secon d->anObj();
} else {
myObj = it->func().first->anObj();
}
myVec.push_back (anObj);
}
where it->func() returns a pair of objects' pointers with 'anObj'
functions which return an object suitable for insertion in myVec.
Now, given that 'global' is known before I enter any loops, I could in
principle decide to use the 'first' or 'second' before I ever enter this
loop, instead of deciding inside the loop (and hopefully thus save a few
ns of time)...
So what is wrong with
if ( global == 0 ) {
// use second fields
for(it(vec.begi n(), end(vec.end()), it!=end;++it){
myVec.push_back ( it->func().secon d->anObj() );
}
else {
// use first fields
for(it(vec.begi n(), end(vec.end()), it!=end;++it){
myVec.push_back ( it->func().first->anObj() );
}
}
[snip]
Best
Kai-Uwe Bux
In article
<d7************ *************** *******@d77g200 0hsb.googlegrou ps.com>,
Triple-DES <De**********@g mail.comwrote:
On 11 Jun, 11:32, shaun roe <shaun....@wana doo.frwrote:
Question about pointer-to-data members
I have a deeply nested loop, the innermost bit looks a little like this:
for(it(vec.begi n(), end(vec.end()), it!=end;++it){
* if (global == 0){
* * myObj = it->func().secon d->anObj();
* } else {
* * myObj = it->func().first->anObj();
* }
* myVec.push_back (anObj);
}
[snip]
>
I think I understand what you are trying to do, but I don't think it's
possible, because there is no special "pointer to data-member".
ok, thanks... I'm using 'C++ Common Knowledge',S. C. Dewhurst, Item 15
:Pointers to Class Members Are Not Pointers
as a reference, where I see:
class C{
public:
int a_;
};
intC::*pimC;
C aC;
C *pC = &aC;
aC.*pimC=0;
int b=pC->*pimC;
so I was hoping to do the same for a pair; however If I understand your
comment below, this is not possible where there is a non-trivial
constructor...t his is an important caveat!
cheers
s
>
Such a pointer would on most implementations be equivalent to the byte
offset from the beginning of the object's data structure to the data
member, IOW very similar to the "offsetof" macro.
However, you can't (formally) use offsetof on a pair, because it is
not strictly POD (it has a user-declared ctor). It would still
probably work on most implementations .
Other options include writing the loop twice:
if(global == 0)
// entire loop
else
// entire loop
Or rewrite the loop using functors to fetch the object, something like
this:
if(global == 0)
std::transform( vec.begin(), vec.end(), std::back_inser ter(myVec),
get1st());
else
std::transform( vec.begin(), vec.end(), std::back_inser ter(myVec),
get2nd());
Which may or may not be faster than the original loop ;)
DP
shaun roe wrote:
In article
<d7************ *************** *******@d77g200 0hsb.googlegrou ps.com>,
Triple-DES <De**********@g mail.comwrote:
>On 11 Jun, 11:32, shaun roe <shaun....@wana doo.frwrote:
Question about pointer-to-data members
I have a deeply nested loop, the innermost bit looks a little like
this:
for(it(vec.begi n(), end(vec.end()), it!=end;++it){
if (global == 0){
myObj = it->func().secon d->anObj();
} else {
myObj = it->func().first->anObj();
}
myVec.push_back (anObj);
}
[snip]
>> I think I understand what you are trying to do, but I don't think it's possible, because there is no special "pointer to data-member".
ok, thanks... I'm using 'C++ Common Knowledge',S. C. Dewhurst, Item 15
:Pointers to Class Members Are Not Pointers
as a reference, where I see:
class C{
public:
int a_;
};
intC::*pimC;
C aC;
C *pC = &aC;
aC.*pimC=0;
int b=pC->*pimC;
What's stopping you?
#include <utility>
#include <iostream>
typedef std::pair<int,i ntint_pair;
typedef int int_pair::* int_member_ptr;
int main ( void ) {
int_member_ptr ptr1 = &int_pair::firs t;
int_member_ptr ptr2 = &int_pair::seco nd;
int_pair the_pair ( 2, 3 );
std::cout << the_pair.*ptr1 << '\n';
std::cout << the_pair.*ptr2 << '\n';
}
so I was hoping to do the same for a pair; however If I understand your
comment below, this is not possible where there is a non-trivial
constructor...t his is an important caveat!
Huh?
The real problem is that for pair<T,S>, this will not work since the pointer
has to point to T or to S. Thus, stuff like this can only be done for
a "diagonal" pair<T,T>.
Best
Kai-Uwe Bux
In article <g2**********@a ioe.org>, Kai-Uwe Bux <jk********@gmx .net>
wrote:
[snip]
>
}
so I was hoping to do the same for a pair; however If I understand your
comment below, this is not possible where there is a non-trivial
constructor...t his is an important caveat!
Huh?
The real problem is that for pair<T,S>, this will not work since the pointer
has to point to T or to S. Thus, stuff like this can only be done for
a "diagonal" pair<T,T>.
Best
Kai-Uwe Bux
Indeed I have a 'diagonal' pair, the only structural difference is that
they are const pointers, so I must be awry somewhere in my syntax...
I'll try harder with a simpler example and come back if that doesnt work!
thanks
shaun
On 11 Jun, 23:03, shaun roe <shaun....@wana doo.frwrote:
*Triple-DES <DenPlettf...@g mail.comwrote:
I think I understand what you are trying to do, but I don't think it's
possible, because there is no special "pointer to data-member".
From your citation, it is obvious that this is just plain wrong. At
least my description of its implementation is fairly accurate.
[snip]
so I was hoping to do the same for a pair; however If I understand your
comment below, this is not possible where there is a non-trivial
constructor...t his is an important caveat!
Yes, this is true for the offsetof macro. It is however not true for
pointers to class members (see Kai-Uwe's post). Note that I still
think using std::transform is the most elegant way of accomplishing
your task :)
DP
"Kai-Uwe Bux" <jk********@gmx .netwrote
shaun roe wrote:
Question about pointer-to-data members
I have a deeply nested loop, the innermost bit looks a little like this:
for(it(vec.begi n(), end(vec.end()), it!=end;++it){
Huh? Could it be that you are trying to do the job of the compilers
optimizing pass?
if (global == 0){
myObj = it->func().secon d->anObj();
} else {
myObj = it->func().first->anObj();
}
myVec.push_back (anObj);
}
where it->func() returns a pair of objects' pointers with 'anObj'
functions which return an object suitable for insertion in myVec.
Now, given that 'global' is known before I enter any loops, I could in
principle decide to use the 'first' or 'second' before I ever enter this
loop, instead of deciding inside the loop (and hopefully thus save a few
ns of time)...
So what is wrong with
if ( global == 0 ) {
// use second fields
for(it(vec.begi n(), end(vec.end()), it!=end;++it){
myVec.push_back ( it->func().secon d->anObj() );
}
else {
// use first fields
for(it(vec.begi n(), end(vec.end()), it!=end;++it){
myVec.push_back ( it->func().first->anObj() );
}
}
I'd do it as follows:
for(it(vec.begi n(), end(vec.end()), it!=end;++it)
myVec.push_back (!global ? it->func().secon d->anObj() : it->func().first->anObj());
Adem24 wrote:
"Kai-Uwe Bux" <jk********@gmx .netwrote
>shaun roe wrote:
Question about pointer-to-data members
I have a deeply nested loop, the innermost bit looks a little like
this:
for(it(vec.begi n(), end(vec.end()), it!=end;++it){
Huh? Could it be that you are trying to do the job of the compilers optimizing pass?
if (global == 0){
myObj = it->func().secon d->anObj();
} else {
myObj = it->func().first->anObj();
}
myVec.push_back (anObj);
}
where it->func() returns a pair of objects' pointers with 'anObj'
functions which return an object suitable for insertion in myVec.
Now, given that 'global' is known before I enter any loops, I could in
principle decide to use the 'first' or 'second' before I ever enter
this loop, instead of deciding inside the loop (and hopefully thus save
a few ns of time)...
So what is wrong with
if ( global == 0 ) { // use second fields for(it(vec.begi n(), end(vec.end()), it!=end;++it){ myVec.push_back ( it->func().secon d->anObj() ); } else { // use first fields for(it(vec.begi n(), end(vec.end()), it!=end;++it){ myVec.push_back ( it->func().first->anObj() ); } }
I'd do it as follows:
for(it(vec.begi n(), end(vec.end()), it!=end;++it)
myVec.push_back (!global ? it->func().secon d->anObj() :
it->func().first->anObj());
You don't like white space, do you?
Note that the OP specifically intended to pull the check for global out of
the loop (you even quoted that):
Now, given that 'global' is known before I enter any loops, I could in
principle decide to use the 'first' or 'second' before I ever enter
this loop, instead of deciding inside the loop (and hopefully thus save
a few ns of time)...
Best
Kai-Uwe Bux This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jamie Burns |
last post by:
Hello,
I just did a simple benchmark:
for (xx=0;xx<100000;xx++) {
rDerived* derived = dynamic_cast<rDerived*>(object);
if (derived) derived->setValue(message.data.messageSetInt.value);
}
against:
|
by: dalbosco |
last post by:
Hello,
I am new to STL and I've written the following code
which crash.
Could anyone tell me why this code crash?
Thanks by advance.
--
J-F
#include <iostream>
|
by: Roman Töngi |
last post by:
I want to change a vector in a function. I pass a pointer of it to
the function and append an item. Then I want to print the first
item in the vector. It doesn't work. Can anyone help me?
Thanks
#include <vector>
#include <iostream>
#include "element.h"
using namespace std;
|
by: Helge Moulding |
last post by:
I've tried the following:
<a name="foo" class="foostyle">Text in foo <a href="bar">link in foo</a>
and more text.</a>
I kinda thought that oughta work. But I found that foostyle is applied
only up to the link in foo. After that the style disappears. This
happens in both Mozilla and MSIE. Even adding contextual selectors to
the style doesn't help.
|
by: Eric-Sebastien Lachance |
last post by:
Hey there,
I decided to just create a 100% height and width div that filled the
space over a background header image, and add an onclick event to
redirect to the my index... Doesn't seem to work in FireFox only, just
in IE. Here's the code:
<div id="headerimg"><div style="width: 100%; height: 100%; cursor:
hand; cursor: pointer;"
onclick="top.location.href('http://www.monamouchtim.com/');"> </div></div>
| |
by: Franck |
last post by:
Hello,
in a datagrid, in the ItemDataBound event
in some cells a add some javascript that show up an information
strHref.Append("<A onclick=\"popupValid();\">");
strHref.Append("<span style=\"cursor:pointer\">");
strHref.Append(e.Item.Cells.Text+"</span></A>");
e.Item.Cells.Text=strHref.ToString();
|
by: mkaushik |
last post by:
Hi everyone,
Im just starting out with C++, and am curious to know how "delete
<pointer>", knows about the number of memory locations to free.
I read somewhere that delete frees up space assigned to <pointerby
"new". Does "new" create a list of pointer names and the size of the
memory array they point to? I also read that some compilers may store
the number of consec mem locations a pointer points to, just before the
first data...
|
by: Noah Roberts |
last post by:
What steps do people take to make sure that when dealing with C API
callback functions that you do the appropriate reinterpret_cast<>? For
instance, today I ran into a situation in which the wrong type was the
target of a cast. Of course with a reinterpret_cast nothing complains
until the UB bites you in the ass. It seems to me that there ought to
be a way to deal with these kinds of functions yet still retain some
semblance of type...
|
by: Peter Lee |
last post by:
store std::<map>::iterator is safe if didn't remove that element
Is store std::<mapelement's pointer also safe ?
|
by: JackC |
last post by:
Hi,
If i have a vector<charmaybe 15mb in size, whats the most efficient
way to write out all the elements to an output file?
I have tried this:
for(int z = 0; z < Output_Buffer.size(); z++)
{
write(ostr, (char *) Output_Buffer, 1);
|
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: 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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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: 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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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: 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 we have to send another system
| |