O
Ondrej Spanel
The code below does not compile with .NET 2003, I get folowing error:
w:\c\Pokusy\delegTemplArg\delegTemplArg.cpp(11) : error C2993: 'float' :
illegal type for non-type template parameter 'x'
The error shows compiler is quite confused - x is not a template parameter
at all.
I have found two workarounds, but still I think the code should compile.
Workaround 1: declare member RetType (Type::*member)(float x) const using a
typedef:
typedef RetType (Type::*DelegateFunctionType)(float x) const;
template <class Type, class RetType, DelegateFunctionType>
Workaround 2: ommit the parametr name in the member function prototype:
template <class Type, class RetType, RetType (Type::*member)(float)
const>
Regards
Ondrej
--------------------------------
#include <stdio.h>
class FooA
{
public:
float GetValue1(float x) const {return 1;}
float GetValue2(float x) const {return 2;}
};
/// delegate - member passed via template argument
template <class Type, class RetType, RetType (Type::*member)(float x) const>
class DelegateTF
{
Type &_type;
public:
DelegateTF(Type &type):_type(type){}
RetType operator () (float x) const
{
return (_type.*member)(x);
}
};
int main()
{
FooA a;
DelegateTF<FooA,float,FooA::GetValue2> atd2(a);
printf("atd2 %g\n",atd2(0.5));
getchar();
return 0;
}
w:\c\Pokusy\delegTemplArg\delegTemplArg.cpp(11) : error C2993: 'float' :
illegal type for non-type template parameter 'x'
The error shows compiler is quite confused - x is not a template parameter
at all.
I have found two workarounds, but still I think the code should compile.
Workaround 1: declare member RetType (Type::*member)(float x) const using a
typedef:
typedef RetType (Type::*DelegateFunctionType)(float x) const;
template <class Type, class RetType, DelegateFunctionType>
Workaround 2: ommit the parametr name in the member function prototype:
template <class Type, class RetType, RetType (Type::*member)(float)
const>
Regards
Ondrej
--------------------------------
#include <stdio.h>
class FooA
{
public:
float GetValue1(float x) const {return 1;}
float GetValue2(float x) const {return 2;}
};
/// delegate - member passed via template argument
template <class Type, class RetType, RetType (Type::*member)(float x) const>
class DelegateTF
{
Type &_type;
public:
DelegateTF(Type &type):_type(type){}
RetType operator () (float x) const
{
return (_type.*member)(x);
}
};
int main()
{
FooA a;
DelegateTF<FooA,float,FooA::GetValue2> atd2(a);
printf("atd2 %g\n",atd2(0.5));
getchar();
return 0;
}