On 2008-02-17 15:03:47 -0500, Vaclav Haisman
> Kira Yamato wrote, On 17.2.2008 21:00:
>> Ok, I'm trying to do something funky here, just to see how much the C++
>> syntax can be stretched. Here's the problem I was trying to solve.
>>
>> Suppose you have the following common situation of a class A with two
>> accessor methods for x:
>>
>> class A
>> {
>> public:
>> int get_x() const;
>> void set_x(int);
>> };
>>
>> Normally, we would have the following syntax:
>>
>> // (1)
>> A a;
>> a.set_x(3);
>> int y = a.get_x();
>>
>> However, let's say I want to implement the following syntax instead
>>
>> // (2)
>> A a;
>> a.x = 3;
>> int y = a.x;
>>
>> But I want (2) to expand out to (1) at compile time so that I keep the
>> efficiency also.
>>
>> So, my attempt to try this involved first writing the following template:
>>
>> template
>> class member_accessor : private boost::noncopyable
>> {
>> public:
>> member_accessor(C &c) : c(c) {}
>>
>> operator M() const { return c.*get(); }
>> member_accessor &operator=(const M &n) { c.*set(n); return *this; }
>>
>> private:
>> C &c;
>> };
>>
>> With this template I can support the syntax in (2) with the new class A as:
>>
>> class A
>> {
>> public:
>> int get_x() const;
>> void set_x(int);
>>
>> member_accessor x;
> You must use &A::get_x if you want pointer to member function.
Oh, thank you so much! It works now.
FYI, here's the correct template code:
template
class member_accessor : private boost::noncopyable
{
public:
member_accessor(C &c) : c(c) {}
operator M() const { return (c.*get)(); }
member_accessor &operator=(const M &n) { (c.*set)(n); return *this; }
private:
C &c;
};
Example usage:
class A
{
public:
A() : x(*this) {}
int get_x() const;
void set_x(int);
member_accessor x;
};
Now you can do
A a;
a.x = 3;
y = a.x;
Hmm... I wonder if it is possible to eliminate the template parameter C
from the template definition. Probably not.
> [...]
--
// kira