An overloaded operator& needs the address of its argument

A

Angel Tsankov

How can an overloaded operator& take the address of its argument:

template<typename T>
Smth operator &(T& SomeObject)
{
// The address of SomeObject is needed here
}
 
S

Sylvester Hesp

Angel Tsankov said:
How can an overloaded operator& take the address of its argument:

template<typename T>
Smth operator &(T& SomeObject)
{
// The address of SomeObject is needed here
}

It's argument is always 'this', as you can't define the unary & as a
non-member. And since 'this' is a pointer, you already have it's address :)

- Sylvester Hesp
 
A

Angel Tsankov

--
Angel Tsankov
(e-mail address removed)-sofia.bg
Sylvester Hesp said:
It's argument is always 'this', as you can't define the unary &
as a non-member. And since 'this' is a pointer, you already
have it's address :)

Does the standard say that unary address-of operator must be a
member? If so, where?
 
S

Sylvester Hesp

Angel Tsankov said:
--
Angel Tsankov
(e-mail address removed)-sofia.bg


Does the standard say that unary address-of operator must be a member? If
so, where?

You're absolutely right, I was mistaken.
You could take the address by using a reinterpret_cast to a primitive type
on which the unary & does what you want. boost::addressof does it like that:

template<class T> T* addressof(T& t)
{
return reinterpret_cast<T*>(&const_cast<char&>(reinterpret_cast<const
volatile char&>(t)));
}

- Sylvester Hesp
 
J

Jim Langston

Sylvester Hesp said:
You're absolutely right, I was mistaken.
You could take the address by using a reinterpret_cast to a primitive type
on which the unary & does what you want. boost::addressof does it like
that:

template<class T> T* addressof(T& t)
{
return reinterpret_cast<T*>(&const_cast<char&>(reinterpret_cast<const
volatile char&>(t)));
}

I must be missing something. Why wouldn't

template<class T> T* addressof(T& t)
{
return &*t;
}

work?
 
G

Greg Herlihy

I must be missing something. Why wouldn't

template<class T> T* addressof(T& t)
{
return &*t;
}

Try it out, say, with an int:

template <int> int * addressof(int& t)
{
return &*t; // Error: invalid type argument of 'unary *'
}

Greg
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

I must be missing something. Why wouldn't

template<class T> T* addressof(T& t)
{
return &*t;
}

work?

First you dereference t (which means that T must either be a pointer
of implement operator *) and then you take the address of what was
returned. So if T was a normal pointer then you would return a copy of
t right?

However since the return-type is T* this does not compile for normal
pointers, nor for builtin functions. The only thing I can see this
working for is something like this:

struct Foo {
Foo& operator*() {return *this;}
};

I think you must have forgotten something in your previous post.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top