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