Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS2010 C++ variadic template example

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!

like image 253
BabelFish Avatar asked Jan 21 '11 20:01

BabelFish


2 Answers

Visual C++ 2010 does not support variadic templates.

like image 120
James McNellis Avatar answered Nov 13 '22 08:11

James McNellis


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.

like image 24
Howard Hinnant Avatar answered Nov 13 '22 07:11

Howard Hinnant