Consider this code which is a very trimmed down version of some I've
inherited and am trying to port from windows to g++:
//Begin test1.cpp
class foo { int i; int j; };
class bar
{
bar (int foo::* dataMember)
:offsetof (foo, *dataMember) //Call this Line (A)
{
// int i = offsetof (foo, *dataMember); //Call this Line (B)
}
};
//End test1.cpp
when I try to compile test1.cpp with g++, the compiler has this to say:
test2.cpp: In constructor `bar::bar(int foo::*)':
test2.cpp:6: syntax error before `;' token
A couple of questions then:
(1) Should the compiler complain? I don't have much experience with
using the 'pointer to data member' stuff, but based on the research I've
just done on the net, the code seems reasonable.
(2) If I move the call from the initializer list to the ctor body by
commenting out line (A) and uncommenting line (B), the compiler error
becomes:
test2.cpp: In constructor `bar::bar(int foo::*)':
test2.cpp:8: syntax error before `;' token
test2.cpp:8: declaration of `dataMember' shadows a parameter
test2.cpp:8: syntax error before `;' token
So, it seems the original problem persists which is reasonable but now
there's this additional shadowing issue. It's not clear to me what
parameer 'dataMember' is shadowing here? If anyone could explain this
to me it would be great.
Thanks in advance for any feedback!
-exits 9 2969
"Exits Funnel" <ex************ ***@yahoo.com> wrote... Consider this code which is a very trimmed down version of some I've inherited and am trying to port from windows to g++:
//Begin test1.cpp class foo { int i; int j; };
class bar { bar (int foo::* dataMember) :offsetof (foo, *dataMember) //Call this Line (A) { // int i = offsetof (foo, *dataMember); //Call this Line (B) } }; //End test1.cpp
when I try to compile test1.cpp with g++, the compiler has this to say:
test2.cpp: In constructor `bar::bar(int foo::*)': test2.cpp:6: syntax error before `;' token
A couple of questions then:
(1) Should the compiler complain? I don't have much experience with using the 'pointer to data member' stuff, but based on the research I've just done on the net, the code seems reasonable.
(2) If I move the call from the initializer list to the ctor body by commenting out line (A) and uncommenting line (B), the compiler error becomes:
test2.cpp: In constructor `bar::bar(int foo::*)': test2.cpp:8: syntax error before `;' token test2.cpp:8: declaration of `dataMember' shadows a parameter test2.cpp:8: syntax error before `;' token
So, it seems the original problem persists which is reasonable but now there's this additional shadowing issue. It's not clear to me what parameer 'dataMember' is shadowing here? If anyone could explain this to me it would be great.
'offsetof' is reserved and defined only when used with POD. Your class
is not POD, so using 'offsetof' with it is essentially prohibited.
Use pointers to members, if you have to, but you'll probably be better off
if you rewrite the algorithm that was written to require 'offsetof'.
As to your particular code, 'offsetof' is a macro that usually expands into
a constant integral expression. What is the constant integral expression
doing in the initialiser list of that constructor? It's like writing
class bar {
bar(int blah) : 42 {}
};
What's the "42" for? What are you trying to do with it?
Victor
> 'offsetof' is reserved and defined only when used with POD. Your
class is not POD, so using 'offsetof' with it is essentially prohibited.
That was my first reaction too, but it's incorrect. He's calling
offsetof _in_ the ctor of bar (and having an explicit ctor means bar
isn't a POD type), but the argument he's passing is foo -- and foo IS a
POD type.
The problem is that he's passing a _pointer_ to a member where offsetof
expects the _name_ of the member. In C++, offsetof is really more
tolerated than fully supported -- it doesn't work with new-fangled
things like pointers to members.
--
Later,
Jerry.
The universe is a figment of its own imagination.
Thanks for taking the time to reply, Victor. 'offsetof' is reserved and defined only when used with POD. Your class is not POD, so using 'offsetof' with it is essentially prohibited.
This is good to know; I couldn't find much info on offsetof anywhere.
It's not clear to me though what makes foo not a POD. Is it just that
the members are private? I just modified the code to make them public
but I get the same error. Use pointers to members, if you have to, but you'll probably be better off if you rewrite the algorithm that was written to require 'offsetof'.
Hopefully, I'll be able to go this route but it's ALOT of code and I'd
like to get it running sooner rather than later. As to your particular code, 'offsetof' is a macro that usually expands into a constant integral expression. What is the constant integral expression doing in the initialiser list of that constructor?
Good point. Actually, I always try to trim as much of the code away as
possible while still retaining the error before posting to USENET.
Maybe I went a bit too far this time :) The actual code was, of course,
something like this:
bar (int foo::* dataMember)
:myVal (offsetof (foo, *dataMember)) //Call this Line (A)
-exits
"Jerry Coffin" <jc*****@taeus. com> wrote... 'offsetof' is reserved and defined only when used with POD. Your class is not POD, so using 'offsetof' with it is essentially prohibited.
That was my first reaction too, but it's incorrect. He's calling offsetof _in_ the ctor of bar (and having an explicit ctor means bar isn't a POD type), but the argument he's passing is foo -- and foo IS a POD type.
How is it POD if it's a class and the members are private?
V
"Exits Funnel" <ex************ ***@yahoo.com> wrote... [...] Actually, I always try to trim as much of the code away as possible while still retaining the error before posting to USENET. Maybe I went a bit too far this time :) The actual code was, of course, something like this:
bar (int foo::* dataMember) :myVal (offsetof (foo, *dataMember)) //Call this Line (A)
Well, a pointer to member cannot be used without the object.
The expression (*blah) where 'blah' is a pointer to member is
meaningless in C++.
V
On Sat, 08 Jan 2005 12:06:24 -0500, Exits Funnel
<ex************ ***@yahoo.com> wrote in comp.lang.c++: Consider this code which is a very trimmed down version of some I've inherited and am trying to port from windows to g++:
//Begin test1.cpp class foo { int i; int j; };
class bar { bar (int foo::* dataMember) :offsetof (foo, *dataMember) //Call this Line (A) { // int i = offsetof (foo, *dataMember); //Call this Line (B) } }; //End test1.cpp
when I try to compile test1.cpp with g++, the compiler has this to say:
test2.cpp: In constructor `bar::bar(int foo::*)': test2.cpp:6: syntax error before `;' token
A couple of questions then:
(1) Should the compiler complain? I don't have much experience with using the 'pointer to data member' stuff, but based on the research I've just done on the net, the code seems reasonable.
(2) If I move the call from the initializer list to the ctor body by commenting out line (A) and uncommenting line (B), the compiler error becomes:
test2.cpp: In constructor `bar::bar(int foo::*)': test2.cpp:8: syntax error before `;' token test2.cpp:8: declaration of `dataMember' shadows a parameter test2.cpp:8: syntax error before `;' token
So, it seems the original problem persists which is reasonable but now there's this additional shadowing issue. It's not clear to me what parameer 'dataMember' is shadowing here? If anyone could explain this to me it would be great.
Thanks in advance for any feedback!
-exits
offsetof, defined in <stdlib.h> and <cstdlib>, is a macro, not a
function. It does not work on objects at all, but on compile-time
symbols.
The offsetof() macro requires two arguments, the NAME of a structure
(or in C++, POD class) type, and the NAME of a member of that
structure or class. The macro generates, _at compile time_ a value of
type size_t that represents the difference in bytes between the
address of any valid object of that type, and the address of the
specified member within that object.
It is evaluated completely at compile time based on names, never at
run time and can never involve a pointer. The concept of pointers,
let alone dereferencing pointers, does not exist at the early phase of
compilation where macros are expanded.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
On Sat, 08 Jan 2005 16:21:52 -0500 in comp.lang.c++, Exits Funnel
<ex************ ***@yahoo.com> wrote, It's not clear to me though what makes foo not a POD. Is it just that the members are private? I just modified the code to make them public but I get the same error.
In your stripped example, it is just the private members, but I
guess there may be a lot of other things in the real code.
Plain Old Data is any built in or user-defined type that has:
- no user-defined constructor
- no user-defined copy assignment operator
- no user defined destructor
- no virtual functions
- no base class (i.e. it is not a derived class)
- no private or protected non-static data members
- no non-static members of type pointer to member
- no non-static members that are references
- no non-static data members that are not also POD types
But that has little to do with the current problem.
Good point. Actually, I always try to trim as much of the code away as possible while still retaining the error before posting to USENET. Maybe I went a bit too far this time :) The actual code was, of course, something like this:
bar (int foo::* dataMember) :myVal (offsetof (foo, *dataMember)) //Call this Line (A)
Different compilers can be expected to implement 'offsetof' in
slightly different ways, to match their differing object model.
Here is an example of a definition of 'offsetof' ripped from a
compiler that I happened to have handy:
#define offsetof(t,i) ((size_t)((char *)&((t *)0)->i - (char *)0))
You notice the second parameter appears in the context '->i'. When
you substitute the macro argument, you get '->*datamember' , invoking
the ->* operator almost by accident and luck. If the macro had been
written with '-> i' it would still work with a member name, but not
the pointer.
If foo is a POD your luck may hold. If the *datamember pointer
involves any of the magic that is needed to implement member
pointers when an inheritance hierarchy is involved, etc., then it
becomes less likely that '(t *)0' will slide to success.
Either way, '*datamember' is not what 'offsetof' was designed to
support.
> This is good to know; I couldn't find much info on offsetof anywhere. It's not clear to me though what makes foo not a POD. Is it just that
the members are private? I just modified the code to make them public
but I get the same error.
Being private _does_ keep them from being POD (I'm not sure what I was
thinking when I said otherwise) but my original point about the problem
remains -- the real problem is that you're supplying a pointer to a
member but it needs the name of a member instead. The bottom line is
that this technique simply can't work, whether it's applied to a POD
type or not.
--
Later,
Jerry.
The universe is a figment of its own imagination.
David, thanks for taking the time to reply. #define offsetof(t,i) ((size_t)((char *)&((t *)0)->i - (char *)0))
In which header did you find this? It's not clear to me what the point
of the ' - (char *)0' bit is. The only macro definition I could find on
my platform (RedHat Linux) was in stddef.h and looked like this:
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
Actually, I wasn't #including stddef.h which is obviously part of my
problem. I thought that offsetof( ) was a built in part of the language
similar to sizeof. Would one expect to have to explicitly include some
header file to use offsetof?
You notice the second parameter appears in the context '->i'. When you substitute the macro argument, you get '->*datamember' , invoking the ->* operator almost by accident and luck. If the macro had been written with '-> i' it would still work with a member name, but not the pointer.
If foo is a POD your luck may hold. If the *datamember pointer involves any of the magic that is needed to implement member pointers when an inheritance hierarchy is involved, etc., then it becomes less likely that '(t *)0' will slide to success.
Either way, '*datamember' is not what 'offsetof' was designed to support.
This is all very helpful information, thanks again.
-exits This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Hiroki Horiuchi |
last post by:
Hello.
I wrote a program, but g++ warns
a.c:11: warning: invalid access to non-static data member `A::y' of NULL object
a.c:11: warning: (perhaps the `offsetof' macro was used incorrectly)
The program is like below.
class A
{
|
by: Alejo |
last post by:
Hello,
My implementation does not define offsetof, so I have designed a little
program that 'attempts' to find the relative position of a member in its
structure. It just does not work.
Could I get some pointers on what I am doing wrong (apart from being coding
so late at night).
#include <stdlib.h>
|
by: Arthur J. O'Dwyer |
last post by:
As far as I know, C89/C90 did not contain the
now-standard offsetof() macro.
Did C89 mandate that structs had to have a consistent
layout? For example, consider the typical layout of
the following structure:
struct weird
{
int x; /* sizeof(int)==4 here */
|
by: Mark A. Odell |
last post by:
Is there a way to obtain the size of a struct element based only upon its
offset within the struct? I seem unable to figure out a way to do this
(short of comparing every element's offset with <offset>). What I would
like to do is create an API something like this:
#include <stddef.h>
struct MemMap
{
unsigned char apple; // 8 bits on my platform
|
by: Simon Morgan |
last post by:
Hi,
Can somebody please help me grok the offsetof() macro?
I've found an explanation on
http://www.embedded.com/shared/printableArticle.jhtml?articleID=18312031
but I'm afraid it still doesn't make sense to me.
The sticking point seems to be:
| |
by: Fred Zwarts |
last post by:
Consider the following definition:
typedef struct {
int a;
int b;
} s;
Now I have a function
void f (int i) { ... }
|
by: Pawel |
last post by:
Hallo group members.
//p1.cpp
#include <stdio.h>
#include <linux/stddef.h>
struct Person {
int m_age;
char* m_name;
};
|
by: Kavya |
last post by:
offsetof(T,m) (size_t)&(((T*)0)->m)
Why do we always start from 0 in this macro to access the offset of
structure or union. Does standard guarantees that structure and union
reside at address 0? If yes, then what if I have two or more
structures. How can they reside at same address?.
|
by: Imre |
last post by:
Hi
I know that offsetof is basically a C leftover, and only works for POD
types, not classes, so it is recommended that I use pointers to
members instead. However, I have a problem where I don't see how I
should use pointers to members.
Basically, I know how to get a member if I have an object and a
pointer-to-member (obj.*ptr instead of obj + offset), but I don't know
how to do the opposite: getting the object from a member address and...
|
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: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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: 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: 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: 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: 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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |