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.

berak
  • 39,159
  • 9
  • 91
  • 89
harsh
  • 905
  • 1
  • 10
  • 21
  • `template 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. – PaulMcKenzie Nov 28 '15 at 00:46
  • I didn't mean the return type of the function. I meant getConvertFunc returns a function of type BinaryFunc – harsh Nov 28 '15 at 00: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. – PaulMcKenzie Nov 28 '15 at 01:00
  • `invalid conversion from ‘void (*)(float, int)’ to ‘func {aka void (*)(int, int)}`. But how are the function ptrs in convert.cpp typecasted? – harsh Nov 28 '15 at 01:03
  • Please see this: http://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. – PaulMcKenzie Nov 28 '15 at 01:07
  • I am just trying to replicate `(BinaryFunc)GET_OPTIMIZED(cvt8u32f)` which I am unable to understand. – harsh Nov 28 '15 at 01:17
  • Why replicate something that is undefined behavior? You can cast the pointer, but calling the function through that pointer is undefined behavior. The link I posted in the comment discusses this. As to your previous comment "how are the function ptrs in convert.cpp typecasted", you can cast basically anything to anything using a C-style casts. You didn't get a syntax error when you did this, right? And what were the results when you attempted to call the function? See the link. – PaulMcKenzie Nov 28 '15 at 02:11

0 Answers0