Przekazanie funkcji jako parametr

0

Siemka, chce zmierzyć czas wykonania funkcji i nie wiem co jest powodem błędu.
Wyskakuje mi:

Severity Code Description Project File Line Suppression State
Error C2276 '&': illegal operation on bound member function expression

a oto fragment kodu:

unsigned long check_time(void(*func)(std::vector<cv::Mat>, std::vector<std::vector<cv::KeyPoint>>&),
	const std::vector<cv::Mat>& oryginalImages,
	std::vector<std::vector<cv::KeyPoint>>& imagesKeypoints)
{
	cTimer timer;
	timer.start();

	func(oryginalImages, imagesKeypoints);

	return timer.getTime(cTimer::timeUnit::Microseconds);
}

std::vector<cv::Mat> findFeaturesFAST(const std::vector<cv::Mat>& oryginalImages)
{
	auto FASTfeatureDetector = cv::FastFeatureDetector::create(90, true, cv::FastFeatureDetector::TYPE_9_16);
	std::vector<std::vector<cv::KeyPoint>> imagesKeypoints;

	std::vector<cv::Mat> scaledImagesWithKeypoints(oryginalImages.size());
	cloneImageVector(oryginalImages, scaledImagesWithKeypoints);

	std::cout << "FAST: " <<
		check_time(FASTfeatureDetector->detect, oryginalImages, imagesKeypoints)
		<< std::endl;

	drawKeypoints(oryginalImages, imagesKeypoints, scaledImagesWithKeypoints);

	return scaledImagesWithKeypoints;
}
0

Wypadałoby powiedzieć gdzie ten błąd występuje...

check_time(FASTfeatureDetector->detect, oryginalImages, imagesKeypoints)

FASTfeatureDetector->detect nie jest poprawnym syntaxem dla pobrania adresu funkcji należącej do klasy. Ponadto, niestatatyczna funkcja klasy ma inny typ niż ten, którego oczekuje twój wskaźnik na funkcję.

2

Ja tam bym się nie bawił we wskaźniki, tylko przekazał po prostu lambdę:

template <typename Function>
unsigned long check_time(Funciton&& func,
    const std::vector<cv::Mat>& oryginalImages,
    std::vector<std::vector<cv::KeyPoint>>& imagesKeypoints)
{
    cTimer timer;
    timer.start();

    func(oryginalImages, imagesKeypoints);

    return timer.getTime(cTimer::timeUnit::Microseconds);
}

std::vector<cv::Mat> findFeaturesFAST(const std::vector<cv::Mat>& oryginalImages)
{
    auto FASTfeatureDetector = cv::FastFeatureDetector::create(90, true, cv::FastFeatureDetector::TYPE_9_16);
    std::vector<std::vector<cv::KeyPoint>> imagesKeypoints;

    std::vector<cv::Mat> scaledImagesWithKeypoints(oryginalImages.size());
    cloneImageVector(oryginalImages, scaledImagesWithKeypoints);

    const auto func = [&FASTfeatureDetector](auto oryginalImages, auto& imagesKeypoints)
    {
        FASTfeatureDetector->detect(oryginalImages, imagesKeypoints);
    };

    std::cout << "FAST: " <<
        check_time(func, oryginalImages, imagesKeypoints)
        << std::endl;

    drawKeypoints(oryginalImages, imagesKeypoints, scaledImagesWithKeypoints);

    return scaledImagesWithKeypoints;
}
4

Można użyć także variadic template i wejść na jeszcze wyższy poziom uogólnienia.

template<typename F, typename... Args>
auto timing(F&& f, Args&&... args)
{
    auto start = std::chrono::steady_clock::now();
    f(std::forward<Args>(args)...);
    return std::chrono::duration<double>(std::chrono::steady_clock::now()-start).count(); 
};
..............
timing( func, oryginalImages, imagesKeypoints );

1 użytkowników online, w tym zalogowanych: 0, gości: 1