0

BinaryFunc is defined in core.hpp as

typedef void (*BinaryFunc)(const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, Size sz, void*); 

and BinaryFunc getConvertFunc(int sdepth, int ddepth) returns a template

template<typename T, typename DT> static void cvt_( const T* src, size_t sstep, DT* dst, size_t dstep, Size size ) 

What is the point of having cvt_ as template if it's always typecasted as BinaryFunc? In case of cvt8u32f for example, getConvertFunc should return cvt_<short, float>. But because it is typecasted, wouldn't it return cvt_<uchar, uchar>?

Also, I tried to replicate the code in convert.cpp in the following manner :

template<typename TS, typename TD> static void test_(TS src, TD dst) { dst = (TD)(src * dst); cout << src << " " << dst << " " << __func__ << endl; } #define GET_NAME(suffix, stype, dtype) \ void test_##suffix(stype src, dtype dst) \ {\ test_(src, dst); \ } GET_NAME(32f32s, float, int) GET_NAME(32f64f, float, double) int main(int argc, char **argv) { typedef void (*func)(int, int); func f1 = (func)(&test_32f32s); func f2 = (func)(&test_32f64f); f1(2.4f, 1); return 0; } 

I get src = 0 in test_. Why is this happening?

if the function pointer is defined as

typedef void (*func)(float, int); 

I get the desired result for f1.

Edit 1: If I call test_32f32s(2.4f, 1) then as well I get the desired result. But I tried to replicate the BinaryFunc syntax which isn't working.

Edit 2: Using a void* in typedef works somehow. Here's the code

template<typename TS, typename TD> static void test_(TS *src, TD *dst) { *dst = (TD)(*(TS*)src * *(TD*)dst); cout << *src << " " << *dst << " " << __func__ << endl; } #define GET_NAME(suffix, stype, dtype) \ void test_##suffix(stype *src, dtype *dst) \ {\ test_(src, dst); \ } GET_NAME(32f16s, float, int) GET_NAME(32f64f, float, double) typedef void (*func)(void*, void*); func getFunc(int type) { func fns[] = {(func)(test_32f16s), (func)(test_32f64f)}; return fns[type]; } int main(int argc, char **argv) { float tmpf = 10.0f; float *f = &tmpf; int tmpi = 2; int *i = &tmpi; double tmpd = 5.0; double *d = &tmpd; cout << *f << " " << *i << " " << *d << endl; func fnf = getFunc(0); fnf(f, i); func fnd = getFunc(1); fnd(f, d); } 

This is just to understand the style of coding used here.

7
  • template<typename T, typename DT> static void cvt_( const T* src, size_t sstep, DT* dst, size_t dstep, Size size ) Where do you see this returning a template? The return type is void. Maybe the formatting (which I don't like) is throwing you off. Commented Nov 28, 2015 at 0:46
  • I didn't mean the return type of the function. I meant getConvertFunc returns a function of type BinaryFunc Commented Nov 28, 2015 at 0:50
  • When it comes to C-style casting, a lot can be revealed if you removed the cast and see what the compiler errors are. Commented Nov 28, 2015 at 1:00
  • invalid conversion from ‘void (*)(float, int)’ to ‘func {aka void (*)(int, int)}. But how are the function ptrs in convert.cpp typecasted? Commented Nov 28, 2015 at 1:03
  • Please see this: stackoverflow.com/questions/11238653/cast-a-function-pointer That's why I asked to remove the C-style casts. The error that the C++ compiler gives you becomes informative. The compiler knows something smells...What the code is attempting to do is bypass the C++ type-safety. Unless you know exactly what you're doing, why you're doing it, and the implications of doing so, "shutting up" the compiler by issuing a C-style cast should be avoided as much as possible. Commented Nov 28, 2015 at 1:07

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.