C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Can types with address-of operator be used to create std::se

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Teh (tî'pô)
Guest





PostPosted: Mon Jan 26, 2004 12:04 am    Post subject: Can types with address-of operator be used to create std::se Reply with quote



I'm working with a class (not mine) that overrides the address-of
operator (operator &) and I'm trying to construct a set of such
objects. CC and VS6 both croak on this sample but g++ compiles
successfully.
I've simplified the code to this:

#include <set>
class int_ptr {
int i;
public:
int * operator&() { return &i; }
};

std::set<int_ptr> s;

Is there a limitation on sets that prohibit the template argument from
defining operator& ? I couldn't find such a limitation in the
standard.

There's no problem in constructing a vector of the int_ptrs BTW the
problem seems to be in tree.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Jeff Schwab
Guest





PostPosted: Mon Jan 26, 2004 10:45 am    Post subject: Re: Can types with address-of operator be used to create std Reply with quote



Teh (tî'pô) wrote:
Quote:
I'm working with a class (not mine) that overrides the address-of
operator (operator &) and I'm trying to construct a set of such
objects. CC and VS6 both croak on this sample but g++ compiles
successfully.
I've simplified the code to this:

#include <set
class int_ptr {
int i;
public:
int * operator&() { return &i; }
};

std::set
Is there a limitation on sets that prohibit the template argument from
defining operator& ? I couldn't find such a limitation in the
standard.

There's no problem in constructing a vector of the int_ptrs BTW the
problem seems to be in tree.


What's the error message? My guess is that the implementation is trying
something like this:

T t; // "T" is int_ptr.
T* p = &t;


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Keith H Duggar
Guest





PostPosted: Mon Jan 26, 2004 8:20 pm    Post subject: Re: Can types with address-of operator be used to create std Reply with quote



If it means anything, every C++ expert and master I've read highly
recommends against overloading the address-of operator. Try to find
another safer way.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Gennaro Prota
Guest





PostPosted: Mon Jan 26, 2004 8:26 pm    Post subject: Re: Can types with address-of operator be used to create std Reply with quote

On 25 Jan 2004 19:04:23 -0500, "Teh (tî'pô)" <teh (AT) mindless (DOT) com> wrote:

Quote:
I'm working with a class (not mine) that overrides the address-of
operator (operator &) and I'm trying to construct a set of such
objects. CC and VS6 both croak on this sample but g++ compiles
successfully.
I've simplified the code to this:

#include <set
class int_ptr {
int i;
public:
int * operator&() { return &i; }
};

std::set
Is there a limitation [...]

Well, you would have troubles here without an adequate comparison
function, but I'll assume it exists in your "real" code.

Yes, there's an important requirement on the behavior of the &
operator, obfuscated under the (not so familiar) name of
"CopyConstructible". See also

http://std.dkuug.dk/jtc1/sc22/wg21/docs/lwg-closed.html#390


The issued is marked with "Future", but I wouldn't hold my breath.
*If* the address-of requirements will be dropped (but, anyway, why
weren't they put as a separate concept?) then library implementors
will need alternative ways to take the address of objects. Of course
it shouldn't be a problem to play unportable tricks here, but is there
a unique, portable solution? Incidentally, I've recently filed a DR
whose resolution could clear at least if the following


template <typename T> T* addressof(T& v)
{
return reinterpret_cast<T*>(
& const_cast<unsigned char&>(
reinterpret_cast<const volatile unsigned char &>(v)
)
);
}


is required to work on any conforming implementation.



Genny.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Allan W
Guest





PostPosted: Wed Jan 28, 2004 2:14 pm    Post subject: Re: Can types with address-of operator be used to create std Reply with quote

Quote:
"Teh (tî'pô)" <teh (AT) mindless (DOT) com> wrote:
I'm working with a class (not mine) that overrides the address-of
operator (operator &) and I'm trying to construct a set of such
objects. CC and VS6 both croak on this sample but g++ compiles
successfully.

#include <set
class int_ptr {
int i;
public:
int * operator&() { return &i; }
};
std::set

Gennaro Prota <gennaro_prota (AT) yahoo (DOT) com> wrote
Quote:
Of course
it shouldn't be a problem to play unportable tricks here, but is there
a unique, portable solution?

I would imagine that wrapping int_ptr in a class that doesn't have
address-of, would help...

struct int_ptr_wrapper { int_ptr i; };
std::set<int_ptr_wrapper>s;

Notice that the int_ptr is public. Now the address-of operator gets
the address of the wrapper, and you can use it to access member i.
This has a drastic effect on the syntax of any code that uses the
set, but at least it should compile correctly...

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Gennaro Prota
Guest





PostPosted: Thu Jan 29, 2004 9:50 am    Post subject: Re: Can types with address-of operator be used to create std Reply with quote

On 28 Jan 2004 09:14:13 -0500, [email]allan_w (AT) my-dejanews (DOT) com[/email] (Allan W)
wrote:

Quote:
[...]
Gennaro Prota <gennaro_prota (AT) yahoo (DOT) com> wrote
Of course
it shouldn't be a problem to play unportable tricks here, but is there
a unique, portable solution?

I would imagine that wrapping int_ptr in a class that doesn't have
address-of, would help...

struct int_ptr_wrapper { int_ptr i; };
std::set<int_ptr_wrapper>s;

Notice that the int_ptr is public. Now the address-of operator gets
the address of the wrapper, and you can use it to access member i.
This has a drastic effect on the syntax of any code that uses the
set, but at least it should compile correctly...


But I wasn't talking about client code. I was rather saying that,
since all contained types must be Assignable and CopyConstructible,
library writers are currently authorized to take the address of any
contained object by simply applying the & operator. Should the
CopyConstructible requirements be relaxed they would have to use
different artifacts. That said, I asked if there is (or there will be)
a consensus on a _portable_ way to do that.


PS: BTW, a strict interpretation of table 30 seems to say that you
can't overload operator & at all, since &t and &u are required to
*denote* the address of t and u. But I'm incline to think that as long
as it returns the same value as the built-in version, with the same
type, you can. Otherwise the requirements could have been stated much
more simply. Thoughts?


Genny.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.