Question

As one of the class template parameters I need to use a pointer to member:

template <class Base, typename Member, Member Base::*m>
class MemPtrTestUgly
{
...
};

This needs to be used as

struct S
{
    int t;
}

MembPtrTestUgly <S, int, &S::t> m;

But I want to use it as this:

MemPtrTestNice<S, &S::t> m;

The member type is deduced from the member pointer. I cannot use function template, as the MemPtrTest class is not supposed to be instantiated (there are just some static functions that will be used). Is there a way how to do it in pure C++03 (no Boost or TR1)?

Was it helpful?

Solution

You can use partial specialization and get a pretty nice-looking implementation:

template <typename TMember, TMember MemberPtr>
class MemPtrTest;

template <typename TBase, typename TType, TType TBase::*MemberPtr>
class MemPtrTest<TType TBase::*, MemberPtr>
{
    // ...
};

This would be used as:

MemPtrTest<decltype(&S::t), &S::t> m;

Of course, this requires decltype or an equivalent, if you don't want to implicitly specify the member type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top