【问题标题】:How to wrap all but one template arguments using typedef and variadic templates?如何使用 typedef 和可变参数模板包装除一个模板参数之外的所有模板参数?
【发布时间】:2015-05-28 04:14:07
【问题描述】:

我有一个用 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>;

所以我想知道如何使用 typedefs(而不是继承)创建一个动作。还有I get many errors

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

【问题讨论】:

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


    【解决方案1】:

    您可以使用alias template

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

    【讨论】:

      猜你喜欢
      • 2014-09-08
      • 2019-11-22
      • 1970-01-01
      • 2016-12-01
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      • 2012-01-20
      • 2021-10-01
      相关资源
      最近更新 更多