简体   繁体   中英

class template deduction from pointer to member

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)?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM