Click here to Skip to main content
15,615,896 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How Can I modify template function "sum" to have a compilable code without modifying the main function?

I've already tried almost everything.

What I have tried:

#include <utility>
#include <iostream>

template<typename T, typename... Args>
auto sum(T (*F) (T, const T &), Args&&... args)
// -> decltype(F(std::forward<Args>(args)...)) // for C++ 11
	return F(std::forward<Args>(args)...);

int main()
	auto func = [](int a, int const& b) { return a += b; };

	int i = 4;
	sum(func, i, 2);
	std::cout << i << std::endl;
	return 0;
Updated 29-Mar-22 0:43am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900