I have this template :
template <class SourceFormat, class DestFormat, void (*convert)(DestFormat, SourceFormat)>
static void _draw(...);
And these functions :
template <class Class1, class Class2>
inline static void convertNone(Class1& dest, Class2& source) {
dest = source;
};
inline static void convertARGB_GREY(unsigned __int32& dest, unsigned __int8& source) {
dest = source + (source << 8);
dest += (dest << 16);
};
I use the template in another function :
void Blitter::draw(...) {
if (...) {
_draw<unsigned __int32, unsigned __int32, &convertNone>(...);
} else {
_draw<unsigned __int32, unsigned __int8, &convertARGB_GREY>(...); // ERRORS go here!
}
}
I get these errors :
Error 1 error C2440: 'specialization' : cannot convert from 'void (__cdecl *)(unsigned int &,unsigned char &)' to 'void (__cdecl *const )(unsigned char,unsigned int)' d:\projects\fanlib\source\blitter.cpp 102
Error 2 error C2973: 'FANLib::Blitter::_draw' : invalid template argument 'void (__cdecl *)(unsigned int &,unsigned char &)' d:\projects\fanlib\source\blitter.cpp 102
I suppose it’s rather obvious that I don’t fully comprehend functions-as-parameters… 🙁
Many thanks in advance
I don’t know if you’ve done it intentionally, but your template parameters go Source/Destintaion and then Destination/Source.
Notice that when you do
_draw<unsigned __int32, unsigned __int8, &convertARGB_GREY>(...);your template definition fills them in as:SourceFormat = unsigned __int32DestFormat = unsigned __int8void (*convert)(unsigned __int8, unsigned __int32)You don’t have a function of that definition though.
You do have one that matches
void (*convert)(unsigned __int32&, unsigned __int8&)Do you see how the parameters don’t match?
Declare your template like this:
and your function declaration like this:
and it will compile. (Since you lose the references, I’d recommend returning the result in this case:
unsigned __int8 destination convertARGB_GREY(...)though.)