|
Boost Users : |
From: Filip KonviÄka (filip.konvicka_at_[hidden])
Date: 2007-06-28 02:56:58
>> I'm just curious - is there a reason why "member" takes 3 template
>> parameters, and not just one, like in
>>
>> member<&Peer::userid>
>>
>> ? The first two types can be extracted from the member pointer, so I
>> find this a bit redundant...
>>
>
> It certainly looks redundant, but it turns out you really cannot do what
> you propose:
>
> template<...>
> struct member;
>
> What should "..." look like? It's not a type parameter, but it cannot
> be made a non-type parameter either since we don't know the type
> of the pointer-to-member being passed...
>
> In decltype- (or typeof-) enabled compilers a terser syntax could be
> achieved by using some macro hackery:
>
> template<typename PtrToMemberType,PtrToMemberType PtrToMember>
> struct member;
>
> #define MEMBER(PtrToMember) \
> member<decltype(PtrToMember),PtrToMember>
>
> So maybe when C++0x arrives we can take advantage of this.
>
I use this template in my code:
template<typename T>
struct get_pointer_to_member_details;
template<typename Class, typename Member>
struct get_pointer_to_member_details<Member Class::*> {
typedef Class class_type;
typedef Member member_type;
};
Using this, you can write (and I do) something like
template<typename MemberPointerType>
struct member {
MemberPointerType pMember;
typedef typename
get_pointer_to_member_details<MemberPointerType>::class_type class_type;
typedef typename
get_pointer_to_member_details<MemberPointerType>::member_type result_type;
member(MemberPointerType pMember) : pMember(pMember) {}
// ...
}
I wonder then, is it MSVC 8 that allows something non-standard, or is it
OK? I think I tried with recent gcc as well and it worked...
Cheers,
Filip
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net