-
October 24th, 2013, 03:40 AM
#1
Variadic template function parameters and method pointers.
Hello there,
I'm new here, hope I have posted to the appropriate place. I have been experimenting with variadic templates with the aim of caching a call to a class method by storing away the object pointer, method pointer and parameters. I've actually had some reasonable success but have now hit a stumbling block. I now wish to wrap my parameters in a simple template class when I cache them. My success is as follows:
Using variadic template functions to store these pointers and paremeters;
- I'm able to pass a method pointer and unwrapped parameters
- I'm able to pass wrapped parameters on their own.
- I'm NOT able to pass a method pointer and wrapped parameters
I set up a little prototype project to demonstrate the issue and added comments above the function calls to indicate the compilation results. Here is the code:
Code:
#include "stdafx.h"
//////////////////////////////////////////////////
// Basic class with a simple method
//////////////////////////////////////////////////
class MyClass
{
public:
char Method( int i, float f )
{
return 'A';
}
};
//////////////////////////////////////////////////
// Simple template wrapper class
//////////////////////////////////////////////////
template < typename classType >
class MyTemplateWrapper
{
public:
MyTemplateWrapper( const classType& initVal ) :
mVal( initVal )
{
}
private:
classType mVal;
};
//////////////////////////////////////////////////
// Template function that takes a method pointer
// and parameters.
// (Able to call)
//////////////////////////////////////////////////
template < typename returnType, typename classType, typename... argTypes >
void TakeMethodAndParams( returnType( classType::*fnPtr )( argTypes... ), argTypes... args )
{
// Do nothing
}
//////////////////////////////////////////////////
// Template function that takes some wrapped
// params.
// (Able to call)
//////////////////////////////////////////////////
template < typename returnType, typename classType, typename... argTypes >
void TakeWrappedParams( MyTemplateWrapper<argTypes>... args )
{
// Do nothing
}
//////////////////////////////////////////////////
// Template function that takes a method pointer
// and wrapped params.
// (Unable to call)
//////////////////////////////////////////////////
template < typename returnType, typename classType, typename... argTypes >
void TakeMethodAndWrappedParams( returnType( classType::*fnPtr )( argTypes... ), MyTemplateWrapper<argTypes>... args )
{
// Do nothing
}
//////////////////////////////////////////////////
// Main
//////////////////////////////////////////////////
int _tmain(int argc, _TCHAR* argv[])
{
// Compiles!
TakeMethodAndParams< char, MyClass, int, float >( &MyClass::Method, 7, 123.0f );
// Compiles!
TakeWrappedParams< char, MyClass, int, float >( 123, 456.0f );
// Error C2660: 'TakeMethodAndWrappedParams' : function does not take 3 arguments
TakeMethodAndWrappedParams< char, MyClass, int, float >( &MyClass::Method, 7, 123.0f );
// Error C2660: 'TakeMethodAndWrappedParams' : function does not take 3 arguments
MyTemplateWrapper< int > intWrapper( 7 );
MyTemplateWrapper< float > floatWrapper( 123.0f );
TakeMethodAndWrappedParams< char, MyClass, int, float >( &MyClass::Method, intWrapper, floatWrapper );
return 0;
}
As you can see, the last two function calls return the following error:
// Error C2660: 'TakeMethodAndWrappedParams' : function does not take 3 arguments
But I'm convinced it should take three arguments, the method pointer and two wrapped parameters. Visual studio even suggested it should as shown below:
So I'm quite confused by this as you can imagine...
Thanks very much for reading, any thoughts would be much appreciated
Talaxy
-
October 24th, 2013, 05:45 AM
#2
Re: Variadic template function parameters and method pointers.
I'd say it's a compiler bug ... anyway, it seems to me you don't want to automatically deduce the types, so, as a workaround, you can try:
Code:
template < typename returnType, typename classType, typename... argTypes >
struct WorkAround{ typedef returnType( classType::*type )(argTypes...); };
template < typename returnType, typename classType, typename... argTypes >
void TakeMethodAndWrappedParams( typename WorkAround<returnType,classType,argTypes...>::type fnPtr, MyTemplateWrapper<argTypes>... args )
...
the online vc++ compiler seems happy with this.
-
October 24th, 2013, 05:56 AM
#3
Re: Variadic template function parameters and method pointers.
That's correct, I am willing to specify the types per function function call. That work around is a nice idea, thanks for that I'll give it a try later!
I eventually intend to wrap this whole call up in a more elegant macro. Hopefully I won't run into the same problem when I try to pass the method pointer and wrapped parameters to the macro, but I think it should work.
Many Thanks
Talaxy
-
October 24th, 2013, 02:24 PM
#4
Re: Variadic template function parameters and method pointers.
Bingo! It worked a treat. An by giving the "WorkAround" struct a constructor that takes the function pointer, I was actually able to just call the function as before, by directly passing the function pointer.
Cheers!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|