Howto: c++ Function Pointer with default values

2020-03-12 04:17发布

问题:

I have:

typedef void (*RespExtractor) (const cv::Mat & image, cv::Mat & resp);

virtual void predict_image(const cv::Mat & src,
            cv::Mat & img_detect,cv::Size patch_size,
            RespExtractor );

void create_hough_features(const cv::Mat & image, cv::Mat & resp, FeatureParams & params =   FeatureParams() );

How would i define the RespExtractor to accept a function with default parameters, such i can call:

predict_image(im_in,im_out,create_hough_features);

I tried following, with no succes:

typedef void (*RespExtractor) (const cv::Mat & image, cv::Mat & resp,FeatureParams params, FeatureParams()); 

回答1:

Function pointers themselves can't have default values. You'll either have to wrap the call via the function pointer in a function that does have default parameters (this could even be a small class that wraps the function pointer and has an operator() with default paremeters), or have different function pointers for the different overloads of your functions.



回答2:

Default parameters aren't part of the function signature, so you can't do this directly.

However, you could define a wrapper function for create_hough_features, or just a second overload that only takes two arguments:

void create_hough_features(const cv::Mat & image, cv::Mat & resp, FeatureParams & params) {
    // blah
}

void create_hough_features(const cv::Mat & image, cv::Mat & resp) {
    create_hough_features(image, resp, DefaultParams());
}