【问题标题】:How do you typedef a function pointer type with parameter pack arguments你如何 typedef 带有参数包参数的函数指针类型
【发布时间】:2017-04-12 20:32:46
【问题描述】:

将参数打包到函数指针中的语法是什么?

我希望能够对函数指针进行 typedef,但是当我执行此类操作时编译器会报错

template< class ...Args >
struct method { typedef typename void(*type)(void*, Args...); };

带有类似error: expected nested-name-specifier before 'void'的消息

【问题讨论】:

  • 见鬼,对不起伙计们。我从编译器中误读了一个错误,上面写着error: need 'typename' before ...,但我把它放在结构中而不是对依赖类型的引用:/

标签: c++ c++11 templates function-pointers variadic-templates


【解决方案1】:

没有typename 也能正常工作。 http://coliru.stacked-crooked.com/a/64b3fbec9276dd70

你不应该在这里使用typename,因为没有nested-name-specifier

【讨论】:

    【解决方案2】:

    我想您应该从 typedef 行中删除 typename

    template <typename ... Args>
    struct method
     { typedef void(*type)(void*, Args...); };
    

    另一种解决方案可能是使用using 而不是typedef(恕我直言)

    template <typename ... Args>
    struct method
     { using type = void(*)(void*, Args...); };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      • 2014-01-17
      • 2019-08-24
      • 2015-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多