VS2010 C++可变参数模板示例

发布于 2024-10-13 11:45:12 字数 861 浏览 4 评论 0原文

我有一个类模板,但我似乎无法弄清楚如何执行可变模板样式实例化。

这是到目前为止我正在寻找的“代码”:

template<typename _Classname, typename... Args>
class CFunctorStartExT 
{
  friend class CXXFactory;
protected:
  template<typename U>
  CFunctorStartExT(typename U& _functor, Args&... args) :
    m_Functor(_functor),
    m_args(args)
  {
  }
  virtual bool ProcessLoop(CSomeClass* pThread)
  {
    return m_Functor(pThread, m_args);
  }

protected:
  _Classname& m_Functor;
  Args... m_args;
};

显然这不会编译:)。这个想法是创建一个类,可以存储构造函数中传入的值(如果有的话......它可能只定义了 _Classname/U),以便稍后可以检索它们以传递给另一个函数中的 m_Functor。

第一:Variadic Template 可以在 VS2010 中完成吗?我仅在 template行中的模板声明 error C2143: syntax error : Missing ',' before '...' 中遇到编译问题

其次,我想要完成的事情可以完成吗?谢谢!

I have a class template and I can't seem to figure out how to perform a Variadic Template style instantiation.

Here is the "code" so far of what I'm looking for:

template<typename _Classname, typename... Args>
class CFunctorStartExT 
{
  friend class CXXFactory;
protected:
  template<typename U>
  CFunctorStartExT(typename U& _functor, Args&... args) :
    m_Functor(_functor),
    m_args(args)
  {
  }
  virtual bool ProcessLoop(CSomeClass* pThread)
  {
    return m_Functor(pThread, m_args);
  }

protected:
  _Classname& m_Functor;
  Args... m_args;
};

Obviously this won't compile :). The idea is to create a class that can store the values passed in (if any.. it might just have _Classname/U defined) on the constructor so they can be retrieved later to pass to m_Functor in another function.

First: can Variadic Template even be done in VS2010? I am getting compile problems just with the template declaration error C2143: syntax error : missing ',' before '...' from the line template<typename _Classname, typename... Args>

Second, can what I am trying to accomplish be done? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

遇见了你 2024-10-20 11:45:12

Visual C++ 2010 不支持可变参数模板。

Visual C++ 2010 does not support variadic templates.

高跟鞋的旋律 2024-10-20 11:45:12

我相信以下内容会满足您的要求。首先你需要一个实用程序:

// make_tuple_indices

template <size_t...> struct tuple_indices {};

template <size_t _Sp, class _IntTuple, size_t _Ep>
struct make_indices_imp;

template <size_t _Sp, size_t ..._Indices, size_t _Ep>
struct make_indices_imp<_Sp, tuple_indices<_Indices...>, _Ep>
{
    typedef typename make_indices_imp<_Sp+1, tuple_indices<_Indices..., _Sp>, _Ep>::type type;
};

template <size_t _Ep, size_t ..._Indices>
struct make_indices_imp<_Ep, tuple_indices<_Indices...>, _Ep>
{
    typedef tuple_indices<_Indices...> type;
};

template <size_t _Ep, size_t _Sp = 0>
struct make_tuple_indices
{
    static_assert(_Sp <= _Ep, "make_tuple_indices input error");
    typedef typename make_indices_imp<_Sp, tuple_indices<>, _Ep>::type type;
};

然后你可以使用它来帮助你扩展保存你的参数的元组:

template<typename _Classname, typename... Args>
class CFunctorStartExT 
{
  friend class CXXFactory;
protected:
  template<typename U>
  CFunctorStartExT(U& _functor, Args&... args) :
    m_Functor(_functor),
    m_args(args...)
  {
  }

  virtual bool ProcessLoop(CSomeClass* pThread)
  {
    return ProcessLoop(pThread,
                       typename make_tuple_indices<sizeof...(Args)>::type());
  }

protected:
  _Classname& m_Functor;
  std::tuple<Args...> m_args;

private:
    template <std::size_t ...Indx>
    bool ProcessLoop(CSomeClass* pThread, tuple_indices<Indx...>)
    {
        return m_Functor(pThread, std::get<Indx>(m_args)...);
    }
};

就VS2010可变参数模板支持而言:我不知道。

I believe the following will do what you want. First you need a utility:

// make_tuple_indices

template <size_t...> struct tuple_indices {};

template <size_t _Sp, class _IntTuple, size_t _Ep>
struct make_indices_imp;

template <size_t _Sp, size_t ..._Indices, size_t _Ep>
struct make_indices_imp<_Sp, tuple_indices<_Indices...>, _Ep>
{
    typedef typename make_indices_imp<_Sp+1, tuple_indices<_Indices..., _Sp>, _Ep>::type type;
};

template <size_t _Ep, size_t ..._Indices>
struct make_indices_imp<_Ep, tuple_indices<_Indices...>, _Ep>
{
    typedef tuple_indices<_Indices...> type;
};

template <size_t _Ep, size_t _Sp = 0>
struct make_tuple_indices
{
    static_assert(_Sp <= _Ep, "make_tuple_indices input error");
    typedef typename make_indices_imp<_Sp, tuple_indices<>, _Ep>::type type;
};

Then you can use this to help you expand a tuple holding your arguments:

template<typename _Classname, typename... Args>
class CFunctorStartExT 
{
  friend class CXXFactory;
protected:
  template<typename U>
  CFunctorStartExT(U& _functor, Args&... args) :
    m_Functor(_functor),
    m_args(args...)
  {
  }

  virtual bool ProcessLoop(CSomeClass* pThread)
  {
    return ProcessLoop(pThread,
                       typename make_tuple_indices<sizeof...(Args)>::type());
  }

protected:
  _Classname& m_Functor;
  std::tuple<Args...> m_args;

private:
    template <std::size_t ...Indx>
    bool ProcessLoop(CSomeClass* pThread, tuple_indices<Indx...>)
    {
        return m_Functor(pThread, std::get<Indx>(m_args)...);
    }
};

As far as VS2010 variadic template support: I have no idea.

萌化 2024-10-20 11:45:12

可变参数模板是一个黑客拼凑而成的补丁——你不会喜欢这个。做到这一点的方法(我突然想到)是将模板专门化与继承结合使用。沿着这些思路:

template<typename Classname, typename... Args>
class CFunctorStartExT;

template<typename Classname, typename Arg0, typename... Args>
class CFunctorStartExT : private CFunctorStartExT<Classname, Args...> {
protected:
  Arg0 m_arg;
};

template<typename Classname>
class CFunctorStartExT {
protected:
  Classname &m_Functor;
};

我以前从未这样做过,也没有测试过,但这是总体思路。您可以查看 std::tuple 实现以了解实际有效的内容。

Variadic templates are a patch upon a kludge upon a hack -- you're not going to enjoy this. The way to do this (off the top of my head) is to use template specialization together with inheritance. Something along these lines:

template<typename Classname, typename... Args>
class CFunctorStartExT;

template<typename Classname, typename Arg0, typename... Args>
class CFunctorStartExT : private CFunctorStartExT<Classname, Args...> {
protected:
  Arg0 m_arg;
};

template<typename Classname>
class CFunctorStartExT {
protected:
  Classname &m_Functor;
};

I've never done this before, and haven't tested it, but this is the general idea. You could have a look at a std::tuple implementation for something that actually works.

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