 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
glenlow@pixelglow.com Guest
|
Posted: Tue Dec 13, 2005 6:13 pm Post subject: Compile-time scoped selection of assignment operator |
|
|
Hi All
Apologies for the unwieldy title -- couldn't come up with something
more succinct or snappy.
Is it possible using Standard C++ to create a scope (without the client
use of macros) that selects a different assignment operator at compile
time based on the scope? i.e. in pseudo-C++ code:
{
// scope 1
a = b + c; // calls one sort of operator=
}
{
// scope 2
a = b + c; // calls another sort of operator=
}
Of course, the type of a and of b+c is the same between scopes, and
you're allowed to put in anything to create the scope.
The closest thing I could come up with is to use "using namespace" to
inject at compile time particular namespace members into the scope so
e.g.
{
using namespace one;
a = b + c;
}
{
using namespace two;
a = b + c;
}
But unfortunately operator= has to be a member, and therefore a member
of the type of a, and the usual lookup rules can't get a different
operator=.
The motivation behind this is simple: I want to have operator overloads
(including operator=) mean different things in different contexts
without having to explicitly qualify the operator overload and thus
lose the familiar infix notation.
Is this possible at all?
Cheers,
Glen Low, Pixelglow Software
www.pixelglow.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gene Bushuyev Guest
|
Posted: Wed Dec 14, 2005 6:10 am Post subject: Re: Compile-time scoped selection of assignment operator |
|
|
<glenlow (AT) pixelglow (DOT) com> wrote
| Quote: | Hi All
Apologies for the unwieldy title -- couldn't come up with something
more succinct or snappy.
Is it possible using Standard C++ to create a scope (without the client
use of macros) that selects a different assignment operator at compile
time based on the scope? i.e. in pseudo-C++ code:
{
// scope 1
a = b + c; // calls one sort of operator=
}
{
// scope 2
a = b + c; // calls another sort of operator=
}
Of course, the type of a and of b+c is the same between scopes, and
you're allowed to put in anything to create the scope.
The closest thing I could come up with is to use "using namespace" to
inject at compile time particular namespace members into the scope so
e.g.
{
using namespace one;
a = b + c;
}
{
using namespace two;
a = b + c;
}
But unfortunately operator= has to be a member, and therefore a member
of the type of a, and the usual lookup rules can't get a different
operator=.
The motivation behind this is simple: I want to have operator overloads
(including operator=) mean different things in different contexts
without having to explicitly qualify the operator overload and thus
lose the familiar infix notation.
Is this possible at all?
|
No, you can't do exactly what you want. Which is a good news for most people, as
operator overloading can already be pretty confusing.
Of course, you can use named functions instead of operators defined in
differently in different namespaces. Or you can create proxy classes that have
different implementation of assignment. The proxies may have the same names, but
be in different namespaces. E.g.
struct A {};
A operator + (const A&, const A&);
namespace one
{
class Proxy
{
A& a_;
public:
explicit Proxy(A& a) : a_(a) {}
A& operator = (const A& a) const { ...; return a_; }
};
}
namespace two
{
class Proxy
{
A& a_;
public:
explicit Proxy(A& a) : a_(a) {}
A& operator = (const A& a) const { ...; return a_; }
};
}
void foo()
{
using one::Proxy; // or using two::Proxy
A a, b, c;
(Proxy(a) = b + c);
}
Alternatively, you can create different proxy classes for operator+ and define
several assignment operators in class A for each proxy type. This solution won't
work if there is another operator+ in the namespace where class A is defined.
E.g.,
struct A;
namespace one
{
class Proxy
{
const A& a_;
const A& b_;
public:
Proxy(const A& a, const A& b) : a_(a), b_(b) {}
};
Proxy operator + (const A& a, const A& b) { return Proxy(a,b); }
}
namespace two
{
class Proxy
{
const A& a_;
const A& b_;
public:
Proxy(const A& a, const A& b) : a_(a), b_(b) {}
};
Proxy operator + (const A& a, const A& b) { return Proxy(a,b); }
}
struct A
{
A& operator = (const one::Proxy&) { ...; return *this; }
A& operator = (const two::Proxy&) { ...; return *this; }
};
void foo()
{
A a, b, c;
using namespace one; // or using namespace two;
a = b + c;
}
-- Gene Bushuyev
----------------------------------------------------------------
There is no greatness where there is no simplicity, goodness and truth. ~ Leo
Tolstoy
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
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
|
|