Member to pointer as template argument
-
Hello, is there a convenient way to pass a pointer to a data member so that it is recognized as a template argument? The following does not compile under VC++ 2008:
template
void AddMember(Type Parent::*Member)
{
return true;
}...
AddMember(&TestObject::i);
//error C2783: could not deduce template argument "Member"Alex
-
Hello, is there a convenient way to pass a pointer to a data member so that it is recognized as a template argument? The following does not compile under VC++ 2008:
template
void AddMember(Type Parent::*Member)
{
return true;
}...
AddMember(&TestObject::i);
//error C2783: could not deduce template argument "Member"Alex
Not completely sure... but did you try just this:
void AddMember(Type *Member)
If you're calling a method for it, no need to specify Parent::*Member, where the pointer comes from is up to the caller.
-
Hello, is there a convenient way to pass a pointer to a data member so that it is recognized as a template argument? The following does not compile under VC++ 2008:
template
void AddMember(Type Parent::*Member)
{
return true;
}...
AddMember(&TestObject::i);
//error C2783: could not deduce template argument "Member"Alex
Try this -
template<class Parent, typename Type>
void AddMember(Type Parent::*Member)
{
return true;
}«_Superman_» _I love work. It gives me something to do between weekends.
-
Try this -
template<class Parent, typename Type>
void AddMember(Type Parent::*Member)
{
return true;
}«_Superman_» _I love work. It gives me something to do between weekends.
Thank you for your answers. Please look at this example:
struct TestObject
{
int i;
};template
void test() {}...
test<&TestObject::i>();
This way, I can call the function template so that the member pointer is passed as a (non-type) template argument. However, in my case, I would like that parent and type are template parameters, too:
struct TestObject
{
int i;
};template
void test() {}...
test();
However, this is not very convenient to call test() as you have to specify all template arguments manually. If you pass &TestObj::i as a argument to the function, Parent and Type are deduced - but not the Member... Alex
-
Thank you for your answers. Please look at this example:
struct TestObject
{
int i;
};template
void test() {}...
test<&TestObject::i>();
This way, I can call the function template so that the member pointer is passed as a (non-type) template argument. However, in my case, I would like that parent and type are template parameters, too:
struct TestObject
{
int i;
};template
void test() {}...
test();
However, this is not very convenient to call test() as you have to specify all template arguments manually. If you pass &TestObj::i as a argument to the function, Parent and Type are deduced - but not the Member... Alex
I understand that you want the parent and type to be template parameters. So let me repeat my earlier code snippet -
template<class Parent, typename Type>
void AddMember(Type Parent::*Member)
{
return true;
}Here
Parent
andType
are template parameters and the calling is also convenient -AddMember(&TestObject::i);
HereParent
will be deduced toTestObject
andType
will be deduced toint
. AndMember
will point toi
. I think this is as convenient as it can get.«_Superman_» _I love work. It gives me something to do between weekends.