Topic: void references - why not?
Author: hyrosen@mail.com (Hyman Rosen)
Date: Fri, 25 Apr 2003 18:03:56 +0000 (UTC) Raw View
witoldk wrote:
>> findme *p = rc<findme *>(&rc<unsigned char &>(foo));
> Is there a reason for the reference to be to the type _unsigned_ char?
No. I just tend to use unsigned char for this kind of low
level byte tomfoolery, since that's the type where all bits
count. Plain char would work for this, though. By the way,
the reference for this is 5.2.10/10 and then 5.2.10/7.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: ajo@andrew.cmu.edu ("Arthur J. O'Dwyer")
Date: Sun, 20 Apr 2003 20:06:55 +0000 (UTC) Raw View
[Originally posted in comp.lang.c++]
Over in alt.comp.lang.learn.c-c++, someone has asked the question:
If I have an instance of a class which overloads the unary &
operator, how do I find out that object's address?
Probably, there is a very nice answer to the question, but it
made me think of something which I'd considered about a year
ago -- void references.
[Note since last post: Apparently, standard C++ provides no answer.]
So, completely unrelated to the above question, I'd like to know what
people think of the following addition to the C++ language.
I've completely forgotten what I needed these for at the time, so the
"why this is important" part is conspicuously absent.)
References to void.
The declaration
void & foo = bar;
declares foo to be a 'reference to void', initialized as a
reference to bar. bar may be an lvalue of any object type
[i.e., not functions].
Expressions of type 'reference to void' may be implicitly converted
to any other type 'reference to x', where x is an object type.
Such expressions may also have the unary & operator applied to them,
yielding the appropriate value of type 'pointer to void.'
They may not be otherwise used without an accompanying cast to an
appropriate reference type or object type [the latter being syntactic
sugar]. For example:
int alfred = 5;
void & barry = alfred;
int igor = barry; // error
int jacob = (int &)barry; // legal; barry is converted to 'int &'
int kevin = (int)barry; // legal; barry is converted to 'int &'
int & richter = barry; // legal; barry is converted to 'int &'
unsigned char & cipher = barry; // legal; barry is converted to
// 'unsigned char &'
char james = (int)barry; // barry is converted to 'int &',
// then its value is truncated to 'char'
'void &' parameters to functions behave in the same fashion.
Okay, so I've forgotten why I thought this was clever at the time.
Does anyone see a potential application for void references, or a
contradiction that would have been raised by their introduction into
C++?
-Arthur
class C { void operator &(){} } foo;
void & x = foo;
C * pfoo = (C *) &x;
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: pfefferd@hotmail.co.il ("Daniel Pfeffer")
Date: Mon, 21 Apr 2003 10:49:23 +0000 (UTC) Raw View
""Arthur J. O'Dwyer"" <ajo@andrew.cmu.edu> wrote in message
news:Pine.LNX.4.53L-031.0304201334340.8768@unix46.andrew.cmu.edu...
>
> [Originally posted in comp.lang.c++]
>
> Over in alt.comp.lang.learn.c-c++, someone has asked the question:
>
> If I have an instance of a class which overloads the unary &
> operator, how do I find out that object's address?
>
> Probably, there is a very nice answer to the question, but it
> made me think of something which I'd considered about a year
> ago -- void references.
It is the class designer's responsibility to ensure that overloaded
operators do the "expected thing". It is perfectly legal to write a class as
follows:
class Integer {
public:
Integer(int);
Integer operator +=(const Integer &op) { value_ -= op.value_;
return *this; }
Integer operator -=(const Integer &op) { value_ *= op.value_;
return *this; }
Integer operator *=(const Integer &op) { value_ /= op.value_;
return *this; }
Integer operator /=(const Integer &op) { value_ %= op.value_;
return *this; }
Integer operator %=(const Integer &op) { value_ += op.value_;
return *this; }
private:
int value_;
};
but this obviously violates a reasonable programmer's expectations.
Daniel Pfeffer
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: dave@boost-consulting.com (David Abrahams)
Date: Mon, 21 Apr 2003 11:54:25 +0000 (UTC) Raw View
ajo@andrew.cmu.edu ("Arthur J. O'Dwyer") writes:
> Over in alt.comp.lang.learn.c-c++, someone has asked the question:
>
> If I have an instance of a class which overloads the unary &
> operator, how do I find out that object's address?
>
> Probably, there is a very nice answer to the question
It's only nice after it's been packaged. It's spelled
boost::addressof(x).
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: "terjes."@chello.no (=?ISO-8859-1?Q?Terje_Sletteb=F8?=)
Date: Mon, 21 Apr 2003 13:14:09 +0000 (UTC) Raw View
ajo@andrew.cmu.edu ("Arthur J. O'Dwyer") wrote in message news:<Pine.LNX.4.53L-031.0304201334340.8768@unix46.andrew.cmu.edu>...
> [Originally posted in comp.lang.c++]
>
> Over in alt.comp.lang.learn.c-c++, someone has asked the question:
>
> If I have an instance of a class which overloads the unary &
> operator, how do I find out that object's address?
>
> Probably, there is a very nice answer to the question, but it
> made me think of something which I'd considered about a year
> ago -- void references.
> [Note since last post: Apparently, standard C++ provides no answer.]
Boost has an addressof() function for this
(http://www.boost.org/libs/utility/utility.htm).
Regards,
Terje
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: allan_w@my-dejanews.com (Allan W)
Date: Mon, 21 Apr 2003 20:12:36 +0000 (UTC) Raw View
ajo@andrew.cmu.edu ("Arthur J. O'Dwyer") wrote
> Over in alt.comp.lang.learn.c-c++, someone has asked the question:
>
> If I have an instance of a class which overloads the unary &
> operator, how do I find out that object's address?
// Assuming some or all of this code:
class bar {};
class findme : public bar {
public:
int i;
float operator&() { return 0.0; } // Try to find me!
};
findme foo;
The top-11 ways to find the address of object foo, despite
the fact that class findme has overloaded unary operator&.
The ideal way.
11. Tell the author of class findme, that you need to have a way to
get the address of the object -- ask him to please add an addr
function.
cout << "The findme is at " << foo.addr() << endl;
Other ways that should work:
10. Cast it to a base class that doesn't have unary& defined, and then
take the address.
bar &b = foo;
cout << "The findme is at " << &b << endl;
9. If the object hasn't overloaded operator->, perhaps you can use that?
cout << "The findme is at " << (foo.operator->) << endl;
8. If the object is in an array, take the address of an array.
findme f[1];
cout << "The findme is at" << f << endl;
7. Make it the first element in a structure, and take the address of
that structure
struct {
findme f;
} gg;
cout << "The findme is at " << &gg << endl;
And these ways not guaranteed by the C++ standard:
6. If the object has no virtual functions or virtual bases, and there's
no multiple inheritance anywhere in the heirarchy -- take the address
of the first member variable
cout << "The findme is at " << &f.i << endl;
5. Derive another class from it, and override operator& to do something sane:
class foundme : public findme {
public: findme *operator&() { return this; }
} foo2;
cout << "The findme is at " << &foo2 << endl;
4. Derive another class from it, and use a cast to force your own version
of operator&:
findme * findhim(foundme &f) { return &f; }
cout << "The findme is at "
<< findhim(reinterpret_cast<foundme>(foo)) << endl;
3. Use in-line assembly to capture the address:
findme *ia(findme &f) { lea ax,f; }
cout << "The findme is at " << ia(foo) << endl;
2. Write an assembly-language program that takes the object by reference:
findme * assembly(findme &); // Defined in Assembly.asm
cout << "The findme is at " << findhim(foo) << endl;
1. Write code that calls the user-defined operator&. Run the program in
the debugger. Stop the program just as it's about to call this operator.
Look at the address in memory of the argument. Rewrite the code section
so that it simply uses this address without calling operator&. Allow the
program to finish.
findme *addr;
addr = &foo; // Breakpoint HERE
cout << "The findme is at " << addr << endl;
:-)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: hyrosen@mail.com (Hyman Rosen)
Date: Mon, 21 Apr 2003 21:27:42 +0000 (UTC) Raw View
Allan W wrote:
> The top-11 ways to find the address of object foo, despite
> the fact that class findme has overloaded unary operator&.
How about the correct and portable way?
#define rc reinterpret_cast
findme foo;
findme *p = rc<findme *>(&rc<unsigned char &>(foo));
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: witoldk@optonline.net (witoldk)
Date: Wed, 23 Apr 2003 13:53:25 +0000 (UTC) Raw View
hyrosen@mail.com (Hyman Rosen) wrote in message news:<1050959575.172574@master.nyc.kbcfp.com>...
> Allan W wrote:
> > The top-11 ways to find the address of object foo, despite
> > the fact that class findme has overloaded unary operator&.
>
> How about the correct and portable way?
> #define rc reinterpret_cast
> findme foo;
> findme *p = rc<findme *>(&rc<unsigned char &>(foo));
>
Is there a reason for the reference to be to the type _unsigned_ char?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]