Reputation: 1192
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?
Upvotes: 7
Views: 3418
Reputation: 507403
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);
}
Upvotes: 2
Reputation: 4291
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 );
}
Upvotes: 2
Reputation: 208456
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.
Upvotes: 3
Reputation:
template <class T>
struct Binder
{
template<typename FT>
void AddMatch();
};
struct TestType
{
int i;
};
int main()
{
Binder<TestType> b;
b.AddMatch<decltype(&TestType::i)>();
}
Upvotes: 0