Boost logo

Boost :

From: Rene Rivera (grafik666_at_[hidden])
Date: 2002-11-02 02:35:37


[2002-11-01] Arkadiy Vertleyb wrote:

>"Joel de Guzman" <djowel_at_[hidden]> wrote in message
>news:014f01c28207$3d86edb0$0100a8c0_at_kim...
>>
>> ----- Original Message -----
>> From: "Rene Rivera" <grafik666_at_[hidden]>
>>
>>
>> > [2002-11-01] Arkadiy Vertleyb wrote:
>> >
>> > >Right, but then, how do you call it:
>> > >
>> > >object.getSomething<type>();
>> > >or
>> > >object.getSomething((type*)0);
>> > >
>> > >I wasn't able to use the first syntax, although I was able to do
>similar
>> > >things with non-member functions.
>> >
>> > Yep, your are right... I looked at where I use it, and I use the
>((type*)0)
>> > call also. It just happens that for me that's in another tmplate class,
>so I
>> > don't see the call directly.
>>
>> There are other ways to eliminate the macros. I hope you
>> guys wouldn't just give up and say it can't be done.
>
>There were many times when I PROVED that something can't be done, and then
>actualy did it... Or somebody else did it. :o))

;o)

How 'bout this...

#include <cassert>

class some_class
{
    public:
    some_class() : number(0) { }
    int number;
};

class another_class
{
    public:
    
    template <typename some_type>
    struct some_type_defer { typedef some_type t_; };
    
    template <typename some_type>
    typename some_type_defer<some_type>::t_ & field()
    {
        static some_type test_object;
        return test_object;
    }
};

int main(int argc, char** argv)
{
    some_class c_;
    another_class x_;

    c_.number = 1;
    c_ = x_.field<some_class>();
    assert(c_.number == 0);
    
    c_.number = 2;
    x_.field<some_class>() = c_;
    assert(x_.field<some_class>().number == 2);
}

...It works with g++ 2.96(RedHat) and 3.2.

Aren't templates fun... and twisted ;-)

-- grafik - Don't Assume Anything
-- rrivera_at_[hidden] - grafik_at_[hidden]
-- 102708583_at_icq - Grafik666_at_AIM - Grafik_at_[hidden]


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk