可变参数模板问题
我正在尝试编写一个通用代码来使用其 target() 模板方法来比较 std::functions 。这是我的非通用示例代码:
#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;
}
这可以使用 gcc 4.6.1 -std=c++-x 编译并正常运行。但是,当我尝试编译以下通用 cmp 函数时,编译器失败并显示解析错误代码:
#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;
}
错误代码是:
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
有任何提示吗?
I'm trying to write a generic code for comparing std::functions using its target() template method. Here is my non-generic sample 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'm trying to compile the following generic cmp function the compiler fails with parse 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 are:
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 hints?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能
与下一行类似。
This should possibly be
and similar for the next line.