Variation patterns

I am trying to write general code to compare std :: functions using the target () template method. Here is my non-generic example code:

#include <cstdio> #include <functional> static void bar() {} static void baz() {} bool cmp(std::function<void()> f1, std::function<void()> f2) { void (**t1)() = f1.target<void(*)()>(); void (**t2)() = f2.target<void(*)()>(); return (!t1 && !t2) || (t1 && t2 && *t1 == *t2); } int main(int argc, char *argv[]) { std::function<void()> f1(bar), f2(baz), f3(bar); printf("equal: %d\n", cmp(f1, f3)); printf("non-equal: %d\n", cmp(f1, f2)); return 0; } 

This compiles and runs fine with gcc 4.6.1 -std = C ++ - x. However, when I try to compile the following general cmp function, the compiler does not work with parsing error codes:

 #include <cstdio> #include <functional> static void bar() {} static void baz() {} template<typename Result, typename ... Args> bool cmp(std::function<Result(Args...)> f1, std::function<Result(Args...)> f2) { Result (**t1)(Args...) = f1.target<Result(*)(Args...)>(); Result (**t2)(Args...) = f2.target<Result(*)(Args...)>(); return (!t1 && !t2) || (t1 && t2 && *t1 == *t2); } int main(int argc, char *argv[]) { std::function<void()> f1(bar), f2(baz), f3(bar); printf("equal: %d\n", cmp(f1, f3)); printf("non-equal: %d\n", cmp(f1, f2)); return 0; } 

Error Codes:

 functional.cpp: In function 'bool cmp(std::function<_Res(_ArgTypes ...)>, std::function<_Res(_ArgTypes ...)>)': functional.cpp:11:44: error: expected primary-expression before '(' token functional.cpp:11:46: error: expected primary-expression before ')' token functional.cpp:11:52: error: expected primary-expression before '...' token functional.cpp:11:58: error: expected primary-expression before ')' token functional.cpp:12:44: error: expected primary-expression before '(' token functional.cpp:12:46: error: expected primary-expression before ')' token functional.cpp:12:52: error: expected primary-expression before '...' token functional.cpp:12:58: error: expected primary-expression before ')' token 

Any clues?

+4
source share
1 answer

It could be

 f1.template target<Result(*)(Args...)>() ^^^^^^^^ 

and similar for the next line.

+5
source

All Articles