可变参数模板问题

发布于 2024-11-18 06:23:23 字数 2156 浏览 1 评论 0原文

我正在尝试编写一个通用代码来使用其 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷血 2024-11-25 06:23:23

这可能

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

与下一行类似。

This should possibly be

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

and similar for the next line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文