First of all, I feel like this must have been asked already somewhere, but all my searches proved fruitless. If this is indeed a duplicate of something somewhere, I apologize in advance.
I am trying to mass-benchmark a bunch of functions from OpenCV and to do so I wanted to write a small meta-function that takes the function to run, its parameters (which vary according to the function passed) and essentially sets up the timing and runs the function in a loop.
Since I'm planning to pass lambdas to the meta-function as well later on (to benchmark composition of functions), I thought of using std::function.
This is the code I came up with after reading the parameter pack description:
template<typename ...Ts>
void run_test(std::string test_name, int num_repeats, std::function<void(Ts...)> f, Ts... fargs)
{
std::cout << std::endl << "Starting " << test_name << std::endl;
start_timing(test_name);
for(int i=0; i<num_repeats; i++)
{
f(fargs...);
}
end_timing(num_repeats);
}
As you see, functionality is down to a minimum. start_timing and end_timing are simple helper functions beyond the scope of this question.
In my main, I call:
// im_in defined and loaded elsewhere
cv::Mat out(im_in.size(), CV_8U);
run_test(
"erode 3x3",
100,
cv::erode,
im_in, out, cv::Mat::ones(3,3,CV_8U)
);
Now, if I try to compile this, I get:
error: no matching function for call to 'run_test(const char [10], const int&, void (&)(cv::InputArray, cv::OutputArray, cv::InputArray, cv::Point, int, int, const Scalar&), cv::Mat&, cv::Mat&, cv::MatExpr)'
);
^
note: candidate: template<class ... Ts> void run_test(std::__cxx11::string, int, std::function<void(Ts ...)>, Ts ...)
void run_test(std::string test_name, int num_repeats, std::function<void(Ts...)> f, Ts... fargs)
^~~~~~~~
note: template argument deduction/substitution failed:
note: mismatched types 'std::function<void(Ts ...)>' and 'void (*)(cv::InputArray, cv::OutputArray, cv::InputArray, cv::Point, int, int, const Scalar&) {aka void (*)(const cv::_InputArray&, const cv::_OutputArray&, const cv::_InputArray&, cv::Point_<int>, int, int, const cv::Scalar_<double>&)}'
);
So... what am I doing wrong? Why is it having a type mismatch instead of deducing the types in Ts... from the parameters list as I expected?
Update:
After writing the last question above, I realized it might have problems deducing Ts... for the std::function because the parameter comes before the actual parameter list to expand into Ts.... So, I changed my code as follows (essentialy, I moved the f parameter at the end):
void run_test(std::string test_name, int num_repeats, Ts... fargs, std::function<void(Ts...)> f)
{ ... }
Of course, I also adapted the function call accordingly:
cv::Mat out(im_in.size(), CV_8U);
run_test(
"erode 3x3",
NUM_REPEATS,
im_in, out, cv::Mat::ones(3,3,CV_8U),
cv::erode, // <<<<<<<<<<<<<<<<<<<<<<<
);
Now, if I compile, I get a different error:
error: expected primary-expression before ')' token );
The change in error makes me think the order of the parameters was indeed important. Is, however, this the correct one? If so, what am I doing wrong?
Update2:
Writing the first update, it struck me that probably my assumption that f can take a function and convert it to a std::function was probably wrong. After a quick research, it seems it is.
I tried replacing std::function<void(Ts...)> f with both auto f and auto& f (& compiling with C++14 enabled), but the expected primary-expression error is still there.
For all I could research, I couldn't find a way that allows me to just pass the function relying on the compiler to figure out the types.
I'm thinking of adapting C++17's std::apply function implementation shown here adding my timing loop around the invoke call, but I don't understand that code, so the chance of getting something wrong is high.
You might get rid of std::function (which add overhead BTW), and use generic for f too:
template<typename F, typename ...Ts>
void run_test(std::string test_name, int num_repeats, F f, const Ts&... fargs)
{
std::cout << std::endl << "Starting " << test_name << std::endl;
start_timing(test_name);
for(int i=0; i<num_repeats; i++)
{
f(fargs...);
}
end_timing(num_repeats);
}
I realized it might have problems deducing Ts... for the std::function
As cv::erode is not a std::function, Ts... cannot be deduced from it, but would be from extra parameters.
Your problem is that cv::erode has extra (defaulted) parameters.
so you cannot create the std::function<Ts...> with Ts... deduced from parameters.
To bypass that issue, you might use lambda instead:
run_test(
"erode 3x3",
100,
[&](){ cv::erode(im_in, out, cv::Mat::ones(3,3,CV_8U)); }
);
the error expected primary-expression before ')' is because you left a , after your last parameter
parameter pack must always be the last parameters of a functions
you should probably try this
template<typename Callable, typename ...Ts>
void run_test(std::string test_name, int num_repeats, Callable func, Ts&& ... fargs)
{
std::cout << std::endl << "Starting " << test_name << std::endl;
start_timing(test_name);
for(int i=0; i<num_repeats; i++)
{
func(std::forward<Ts>(fargs)...);
}
end_timing(num_repeats);
}
Related
I'm trying to make a function that takes a variable number of parameters of any type, but even the simple example I made is getting an error
#include <iostream>
#include <functional>
template<class... Ts>
void callFunction(const std::function<void(Ts...)>& function, Ts... parameters)
{
function(parameters...);
}
void myFunc(const std::string& output)
{
std::cout << output << std::endl;
}
int main()
{
callFunction<const std::string&>(&myFunc, "Hello world");
return 0;
}
When I run the above code in Ideone, I get this error:
prog.cpp: In function ‘int main()’:
prog.cpp:17:57: error: no matching function for call to ‘callFunction(void (*)(const string&), const char [12])’
callFunction<const std::string&>(&myFunc, "Hello world");
^
prog.cpp:5:6: note: candidate: template<class ... Ts> void callFunction(const std::function<void(Ts ...)>&, Ts ...)
void callFunction(const std::function<void(Ts...)>& function, Ts... parameters)
^~~~~~~~~~~~
prog.cpp:5:6: note: template argument deduction/substitution failed:
prog.cpp:17:57: note: mismatched types ‘const std::function<void(Ts ...)>’ and ‘void (*)(const string&) {aka void (*)(const std::__cxx11::basic_string<char>&)}’
callFunction<const std::string&>(&myFunc, "Hello world");
A simple suggestion: receive the callable as a deduced typename, not as a std::function
I mean (adding also perfect forwarding)
template <typename F, typename ... Ts>
void callFunction(F const & func, Ts && ... pars)
{ func(std::forward<Ts>(pars)...); }
and, obviously, call it without explicating nothing
callFunction(&myFunc, "Hello world");
This as the additional vantage that avoid the conversion of the callable to a std::function.
Anyway, I see two problems in your code:
1) if you receive the functional as a std::function receiving a list ot arguments types (a variadic list in this case, but isn't important for this problem) as a list of argument of the same types, you have to be sure that the types in the two list match exactly.
This isn't your case because the function receive a std::string const & and you pass as argument a the string literal "Hello world" that is a char const [12] that is a different type.
When the types are to be deduced, this cause a compilation error because the compiler can't choose between the two types.
You could solve receiving two list of types
template <typename ... Ts1, typename Ts2>
void callFunction (std::function<void(Ts1...)> const & function,
Ts2 && ... parameters)
{ function(std::forward<Ts2>(parameters)...); }
but now we have the second problem
2) You pass a pointer function (&myFunc) where callFunction() wait for a std::function.
We have a chicken-egg problem because &myFunc can be converted to a std::function but isn't a std::function.
So the compiler can't deduce the Ts... list of types from &myFunc because isn't a std::function and can't convert &myFunc to a std::function because doesn't know the Ts... type list.
I see that you have explicated the first type in the Ts... list, but isn't enough because the Ts... list is a variadic one so the compiler doesn't know that there is only a type in the Ts... list.
A simple solution to this problem is pass the function as a simple deduced F type.
Otherwise, if you have written callFunction() with two templates types lists, you can pass a std::function to the function
std::function<void(std::string const &)> f{&myFunc};
callFunction(f, "Hello world");
but I don't think is a satisfactory solution.
Using a function that accepts templated functions works great when the type is either an rvalue reference or has no reference, but as soon as I make it an lvalue reference it breaks.
Note that V is currently unused here, but it still fails to compile anyways regardless of whether it's used or not.
using namespace std;
template <typename F, typename V = std::invoke_result_t<F, string>>
void func(F f) {
std::vector<string> v = { "a", "b", "c" };
std::for_each(v.begin(), v.end(), f);
}
int main() {
func([](string s) { return s.length(); }); // Good
// func([](string& s) { return s.length(); }); // Bad
func([](const string& s) { return s.length(); }); // Good
}
main.cpp: In function 'int main()':
main.cpp:18:46: error: no matching function for call to 'func(main()::)'
func([](string& s) { return s.length(); });
^
main.cpp:11:6: note: candidate: 'template void func(F)'
void func(F f) {
^~~~
main.cpp:11:6: note: template argument deduction/substitution failed:
I can't do something like
std::invoke_result_t<F, string&>
and I couldn't do something like
std::invoke_result_t<F, std::add_lvalue_reference_t<string>>
The last one was a shot in the dark. My template knowledge is not that great. I've been searching around on here and on various blogs/google/etc, haven't had much success.
std::invoke_result_t<F, string>
this means passing F a string rvalue. And you cannot if F takes an lvalue reference.
I can't do something like
std::invoke_result_t<F, string&>
well yes you can. Do that if you want to know what the result of calling it with a non-const lvalue is.
At your point of use in your sample code, you pass it an lvalue. The string&& overload does not work.
While exploring templates in C++, I stumbled upon the example in the following code:
#include <iostream>
#include <functional>
template <typename T>
void call(std::function<void(T)> f, T v)
{
f(v);
}
int main(int argc, char const *argv[])
{
auto foo = [](int i) {
std::cout << i << std::endl;
};
call(foo, 1);
return 0;
}
To compile this program, I am using the GNU C++ Compiler g++:
$ g++ --version // g++ (Ubuntu 6.5.0-1ubuntu1~16.04) 6.5.0 20181026
After compiling for C++11, I get the following error:
$ g++ -std=c++11 template_example_1.cpp -Wall
template_example_1.cpp: In function ‘int main(int, const char**)’:
template_example_1.cpp:15:16: error: no matching function for call to ‘call(main(int, const char**)::<lambda(int)>&, int)’
call(foo, 1);
^
template_example_1.cpp:5:6: note: candidate: template<class T> void call(std::function<void(T)>, T)
void call(std::function<void(T)> f, T v)
^~~~
template_example_1.cpp:5:6: note: template argument deduction/substitution failed:
template_example_1.cpp:15:16: note: ‘main(int, const char**)::<lambda(int)>’ is not derived from ‘std::function<void(T)>’
call(foo, 1);
^
(same for C++14 and C++17)
From the compiler error and notes I understand that the compiler failed to deduce the type of the lambda, since it cannot be matched against std::function.
Looking at previous questions (1, 2, 3, and 4) regarding this error, I am still confused about it.
As pointed out in answers from questions 3 and 4, this error can be fixed by explicitly specifying the template argument, like so:
int main(int argc, char const *argv[])
{
...
call<int>(foo, 1); // <-- specify template argument type
// call<double>(foo, 1) // <-- works! Why?
return 0;
}
However, when I use other types instead of int, like double, float, char, or bool, it works as well, which got me more confused.
So, my questions are as follow:
Why does it work when I explicitly specify int (and others) as the template argument?
Is there a more general way to solve this?
A std::function is not a lambda, and a lambda is not a std::function.
A lambda is an anonymous type with an operator() and some other minor utility. Your:
auto foo = [](int i) {
std::cout << i << std::endl;
};
is shorthand for
struct __anonymous__type__you__cannot__name__ {
void operator()(int i) {
std::cout << i << std::endl;
}
};
__anonymous__type__you__cannot__name__ foo;
very roughly (there are actual convert-to-function pointer and some other noise I won't cover).
But, note that it does not inherit from std::function<void(int)>.
A lambda won't deduce the template parameters of a std::function because they are unrelated types. Template type deduction is exact pattern matching against types of arguments passed and their base classes. It does not attempt to use conversion of any kind.
A std::function<R(Args...)> is a type that can store anything copyable that can be invoked with values compatible with Args... and returns something compatible with R.
So std::function<void(char)> can store anything that can be invoked with a char. As int functions can be invoked with a char, that works.
Try it:
void some_func( int x ) {
std::cout << x << "\n";
}
int main() {
some_func('a');
some_func(3.14);
}
std::function does that some conversion from its signature to the callable stored within it.
The simplest solution is:
template <class F, class T>
void call(F f, T v) {
f(v);
}
now, in extremely rare cases, you actually need the signature. You can do this in c++17:
template<class T>
void call(std::function<void(T)> f, T v) {
f(v);
}
template<class F, class T>
void call(F f_in, T v) {
std::function f = std::forward<F>(f_in);
call(std::move(f), std::forward<T>(v));
}
Finally, your call is a crippled version of std::invoke from c++17. Consider using it; if not, use backported versions.
I'm trying to pass an overloaded function pointer to template function as a parameter.
float Function1(float par1)
{
return 0;
}
float Function1(float par1, float par2)
{
return 0;
}
template<typename R, typename A1>
void Bind(R(*func)(A1))
{
std::cout << "Correct one called\n";
}
template<typename R, typename A1, typename A2>
void Bind(R(*func)(A1, A2))
{
std::cout << "False one called\n";
}
int main()
{
Bind<float, float>(&Function1);
}
Even tho i call the function with 2 float parameters explicity, compiler can't seem to resolve the correct call.
Compiler shows an 'ambiguous function call' error.
I have created a small sample here:
http://liveworkspace.org/code/4kVlUY$195
What's the cause of this error?
Thank you.
The ambiguity comes when you try to take Function1's address. The compiler sees 2 overloads and it can't know which one you're referring to. You need to explicitly indicate which one you want:
Bind(
static_cast<float(*)(float, float)>(&Function1)
);
You're indicating template arguments explicitly on the call to Bind, but that's too late, the ambiguity was found before that point.
You need to resolve the ambiguity manually, such as with a cast expression.
Bind<float, float>( static_cast< float (*)(float par1, float par2)>( &Function1 ));
According to the error message, it's not Function1 that's ambiguous, it's Bind.
Compilation finished with errors:
source.cpp:31:4: error: call to 'Bind' is ambiguous
Bind<float, float>(&Function1);
^~~~~~~~~~~~~~~~~~
source.cpp:18:6: note: candidate function [with R = float, A1 = float]
void Bind(R(*func)(A1))
^
source.cpp:24:6: note: candidate function [with R = float, A1 = float, A2 = float]
void Bind(R(*func)(A1, A2))
The problems is that you specified two arguments, <float, float>, but that doesn't exclude the possibility of a third argument which is automatically deduced. C++ allows for explicit and implicit arguments to the same function template call!
Another solution is to force it to resolve the template name without considering implicit arguments. This works too, but it's more hackish:
(*&Bind<float, float>)(&Function1); // Taking address first hides arguments from deduction
This is probably going to be a really simple explanation, but I'm going to give as much backstory as possible in case I'm wrong. Advanced apologies for being so verbose. I'm using gcc4.5, and I realize the c++0x support is still somewhat experimental, but I'm going to act on the assumption that there's a non-bug related reason for the behavior I'm seeing.
I'm experimenting with variadic function templates. The end goal was to build a cons-list out of std::pair. It wasn't meant to be a custom type, just a string of pair objects. The function that constructs the list would have to be in some way recursive, with the ultimate return value being dependent on the result of the recursive calls. As an added twist, successive parameters are added together before being inserted into the list. So if I pass [1, 2, 3, 4, 5, 6] the end result should be {1+2, {3+4, 5+6}}.
My initial attempt was fairly naive. A function, Build, with two overloads. One took two identical parameters and simply returned their sum. The other took two parameters and a parameter pack. The return value was a pair consisting of the sum of the two set parameters, and the recursive call. In retrospect, this was obviously a flawed strategy, because the function isn't declared when I try to figure out its return type, so it has no choice but to resolve to the non-recursive version.
That I understand. Where I got confused was the second iteration. I decided to make those functions static members of a template class. The function calls themselves are not parameterized, but instead the entire class is. My assumption was that when the recursive function attempts to generate its return type, it would instantiate a whole new version of the structure with its own static function, and everything would work itself out.
The result was: "error: no matching function for call to BuildStruct<double, double, char, char>::Go(const char&, const char&)"
The offending code:
static auto Go(const Type& t0, const Type& t1, const Types&... rest)
-> std::pair<Type, decltype(BuildStruct<Types...>::Go(rest...))>
My confusion comes from the fact that the parameters to BuildStruct should always be the same types as the arguments sent to BuildStruct::Go, but in the error code Go is missing the initial two double parameters. What am I missing here? If my initial assumption about how the static functions would be chosen was incorrect, why is it trying to call the wrong function rather than just not finding a function at all? It seems to just be mixing types willy-nilly, and I just can't come up with an explanation as to why. If I add additional parameters to the initial call, it always burrows down to that last step before failing, so presumably the recursion itself is at least partially working. This is in direct contrast to the initial attempt, which always failed to find a function call right away.
Ultimately, I've gotten past the problem, with a fairly elegant solution that hardly resembles either of the first two attempts. So I know how to do what I want to do. I'm looking for an explanation for the failure I saw.
Full code to follow since I'm sure my verbal description was insufficient. First some boilerplate, if you feel compelled to execute the code and see it for yourself. Then the initial attempt, which failed reasonably, then the second attempt, which did not.
#include <iostream>
using std::cout;
using std::endl;
#include <utility>
template<typename T1, typename T2>
std::ostream& operator <<(std::ostream& str, const std::pair<T1, T2>& p) {
return str << "[" << p.first << ", " << p.second << "]";
}
//Insert code here
int main() {
Execute(5, 6, 4.3, 2.2, 'c', 'd');
Execute(5, 6, 4.3, 2.2);
Execute(5, 6);
return 0;
}
Non-struct solution:
template<typename Type>
Type BuildFunction(const Type& t0, const Type& t1) {
return t0 + t1;
}
template<typename Type, typename... Rest>
auto BuildFunction(const Type& t0, const Type& t1, const Rest&... rest)
-> std::pair<Type, decltype(BuildFunction(rest...))> {
return std::pair<Type, decltype(BuildFunction(rest...))>
(t0 + t1, BuildFunction(rest...));
}
template<typename... Types>
void Execute(const Types&... t) {
cout << BuildFunction(t...) << endl;
}
Resulting errors:
test.cpp: In function 'void Execute(const Types& ...) [with Types = {int, int, double, double, char, char}]':
test.cpp:33:35: instantiated from here
test.cpp:28:3: error: no matching function for call to 'BuildFunction(const int&, const int&, const double&, const double&, const char&, const char&)'
Struct solution:
template<typename... Types>
struct BuildStruct;
template<typename Type>
struct BuildStruct<Type, Type> {
static Type Go(const Type& t0, const Type& t1) { return t0 + t1; }
};
template<typename Type, typename... Types>
struct BuildStruct<Type, Type, Types...> {
static auto Go(const Type& t0, const Type& t1, const Types&... rest)
-> std::pair<Type, decltype(BuildStruct<Types...>::Go(rest...))> {
return std::pair<Type, decltype(BuildStruct<Types...>::Go(rest...))>
(t0 + t1, BuildStruct<Types...>::Go(rest...));
}
};
template<typename... Types>
void Execute(const Types&... t) {
cout << BuildStruct<Types...>::Go(t...) << endl;
}
Resulting errors:
test.cpp: In instantiation of 'BuildStruct<int, int, double, double, char, char>':
test.cpp:33:3: instantiated from 'void Execute(const Types& ...) [with Types = {int, int, double, double, char, char}]'
test.cpp:38:41: instantiated from here
test.cpp:24:15: error: no matching function for call to 'BuildStruct<double, double, char, char>::Go(const char&, const char&)'
test.cpp:24:15: note: candidate is: static std::pair<Type, decltype (BuildStruct<Types ...>::Go(BuildStruct<Type, Type, Types ...>::Go::rest ...))> BuildStruct<Type, Type, Types ...>::Go(const Type&, const Type&, const Types& ...) [with Type = double, Types = {char, char}, decltype (BuildStruct<Types ...>::Go(BuildStruct<Type, Type, Types ...>::Go::rest ...)) = char]
test.cpp: In function 'void Execute(const Types& ...) [with Types = {int, int, double, double, char, char}]':
test.cpp:38:41: instantiated from here
test.cpp:33:3: error: 'Go' is not a member of 'BuildStruct<int, int, double, double, char, char>'
Reading the comments, it seems clear enough that this is a very localized bug in a particular version of G++, and that's all the answer there will ever be.