Question

I know this was not possible in C++03, but I'm hoping there is some new voodoo to allow me to do this. See below:

template <class T>
struct Binder
{
    template<typename FT, FT T::*PtrTomember>
    void AddMatch();
};
struct TestType
{
    int i;
};
int main(int argc, char** argv)
{
    Binder<TestType> b;
    b.AddMatch<int,&TestType::i>(); //I have to do this now
    b.AddMatch<&TestType::i>(); //I'd like to be able to do this (i.e. infer field type)
}

Is there any way to do this in C++11? Will decltype help?

** UPDATE: Using Vlad's example I Was thinking something like this would work (caveat: I have not compiled as I am building the compiler with decltype support now)

template <class T>
struct Binder
{
    template<typename MP, FT ft = decltype(MP)>
    void AddMatch()
    {
        //static_assert to make sure MP is a member pointer of T
    }
};
struct TestType
{
    int i;
};
int main()
{
    Binder<TestType> b;
    b.AddMatch<&TestType::i>();  
}

Would this work?

Was it helpful?

Solution

What you are trying to do cannot be done, that is, you cannot use a member pointer as a constant expression unless you have a type. That is, the type of a non-type template argument must be provided, or in other words, there is no type inference for template arguments.

OTHER TIPS

How about this ( as a kicker it works in c++03 ):

#include <iostream>
#include <typeinfo>

template< typename T > struct ExtractMemberTypeHelper;
template< typename R, typename T >
struct ExtractMemberTypeHelper< R(T::*) >
{
    typedef R Type;
    typedef T ParentType;
};

template< typename T >
struct ExtractMemberType : public ExtractMemberTypeHelper< T > {};

struct foo
{
    int bar;
    template< typename T >
    void func( const T& a_Arg )
    {
        std::cout << typeid( typename ExtractMemberType< T >::Type ).name( ) << " " << typeid( typename ExtractMemberType< T >::ParentType ).name( ) << std::endl;
    }
};

int main()
{
    foo inst;
    inst.func( &foo::bar );
}

You could make "T" provide this information.

template <class ...T>
struct BoundTypes { };

template <class U, class T>
struct BinderDecls { 
  void AddMatch();
};

template <class U, class T, class ...Ts>
struct BinderDecls<U, BoundTypes<T, Ts...>>
  :BinderDecls<U, BoundTypes<Ts...>>
{ 
  using BinderDecls<U, BoundTypes<Ts...>>::AddMatch;

  template<T U::*PtrTomember>
  void AddMatch();
};

template <class T>
struct Binder : BinderDecls<T, typename T::bound_types> 
{ }

Then it becomes easy

struct TestType {
    typedef BoundTypes<int, float> bound_types;

    int i;
    float j;
};

int main(int argc, char** argv)
{
    Binder<TestType> b;
    b.AddMatch<&TestType::i>();
    b.AddMatch<&TestType::j>();
}

Alternatively you can use friend function definitions

template <class ...T>
struct BoundTypes { };

template <class U, class T>
struct BinderDecls {
  template<T U::*ptr>
  friend void addMatch(BinderDecl &u) {
   // ...
  }
};

template <class U, class ...Ts>
struct BinderDecls<U, BoundTypes<Ts...>> : BinderDecls<U, Ts>...
{ };

template<typename = void> 
void addMatch() = delete;

template <class T>
struct Binder : BinderDecls<T, typename T::bound_types> 
{ }

Then you can write

struct TestType {
    typedef BoundTypes<int, float> bound_types;

    int i;
    float j;
};

int main(int argc, char** argv)
{
    Binder<TestType> b;
    addMatch<&TestType::i>(b);
    addMatch<&TestType::j>(b);
}
template <class T>
struct Binder
{
    template<typename FT>
    void AddMatch();
};

struct TestType
{
    int i;
};

int main()
{
    Binder<TestType> b;
    b.AddMatch<decltype(&TestType::i)>();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top