template<typename T, typename TriFunc>
TriFunc process_three_continuous_channels_impl(cv::Mat &input, TriFunc func)
{
int const total = input.total();
auto *input_ptr = input.ptr<T>(0);
for(int index = 0; index != total; ++index){
func(*input_ptr, *(input_ptr + 1), *(input_ptr + 2));
input_ptr += 3;
}
return func;
}
/**
* overload, haven't find a good solution to reduce redundancy codes yet
*/
template<typename T, typename TriFunc>
TriFunc process_three_continuous_channels_impl(cv::Mat const &input, TriFunc func)
{
int const total = input.total();
auto const *input_ptr = input.ptr<T>(0);
for(int index = 0; index != total; ++index){
func(*input_ptr, *(input_ptr + 1), *(input_ptr + 2));
input_ptr += 3;
}
return func;
}
我想减少冗余码,但找不到好的解决方案
template<typename T, typename TriFunc>
TriFunc process_three_continuous_channels_impl(cv::Mat const &input, TriFunc func)
{
return process_three_continuous_channels_impl<T>(const_cast<cv::Mat&>(input, func));
}
这不起作用,因为func能够在const_cast之后改变输入数据 将非const cv :: Mat转换为const也无法工作,这样func就无法工作 改变输入数据。有什么建议吗?
答案 0 :(得分:1)
如果C ++ 11是您的选择,那么完美转发在这里工作:
template <typename T, typename TriFunc>
TriFunc wrapper(cv::Mat && input, TriFunc func)
{
return process_three_continuous_channels_impl<T>(
std::forward(input), func);
}
答案 1 :(得分:1)
您可以使cv::Mat
参数成为模板参数类型的引用。
template<typename Mat, typename T, typename TriFunc>
TriFunc process_three_continuous_channels_impl(Mat&& input, TriFunc func)
{
int const total = std::forward<Mat>(input).total();
auto input_ptr = std::forward<Mat>(input).template ptr<T>(0);
for(int index = 0; index != total; ++index){
func(*input_ptr, *(input_ptr + 1), *(input_ptr + 2));
input_ptr += 3;
}
return func;
}