c++ - 如何使用 typedef 和可变参数模板包装除一个模板参数之外的所有模板参数?

标签 c++ c++11 typedef variadic-templates

我有一个用 SWIG 包装的类,它是 std::function 代理:

template <class TR = void, class ... Types>
struct GenericFunc : std::function<TR (Types...)> {
    GenericFunc() {}

    GenericFunc(const std::function<TR (Types... t)> & Action) : std::function<TR (Types... t)>(Action) {}

    virtual TR OnAction(Types ... result) {
        if(*this) {
            return (*this)(result...);
        }
        return TR();
    }
    virtual ~GenericFunc(){}
};

在 C# 或 Java 中,可以重载 OnAction 并获得他想要的结果,而在 C++ 中,我们只有一个额外的检查。

拥有 Func 的人会喜欢拥有 Action - 一个返回 void 的函数。

typedef GenericFunc<void,  Types...> GenericAction<class ... Types>;

所以我想知道如何使用 typedef(而不是继承)来创建一个 Action 。并且 I get many errors

如何通过 C++11 和 typedef 包装某些类型?

最佳答案

您可以使用 alias template .

template<class... Types>
using GenericAction = GenericFunc<void, Types...>;

关于c++ - 如何使用 typedef 和可变参数模板包装除一个模板参数之外的所有模板参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30497139/

相关文章:

C++ RegSetValueEx 出现问题

c++ - 我不明白为什么我会收到 Bad access exception

c++ - 尝试运行 FEniCS 示例代码时编译器出错

c++11 - 如何在 CMake 中激活 C++ 11?

c++ - 将 nullptr 传递给可变模板指针

c++ - 声明指向成员函数的可变参数模板

c++ - 在实现文件中使用 typedef 来缩短类型签名的负面后果是什么?

c++ - 通用正则表达式变音解决方案?

c - 在多个 .c 和 .h 文件中使用 struct typedef

c++ - typedef 和 using 有什么区别?