VS2010 C++可变参数模板示例
我有一个类模板,但我似乎无法弄清楚如何执行可变模板样式实例化。
这是到目前为止我正在寻找的“代码”:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Visual C++ 2010 不支持可变参数模板。
Visual C++ 2010 does not support variadic templates.
我相信以下内容会满足您的要求。首先你需要一个实用程序:
然后你可以使用它来帮助你扩展保存你的参数的元组:
就VS2010可变参数模板支持而言:我不知道。
I believe the following will do what you want. First you need a utility:
Then you can use this to help you expand a tuple holding your arguments:
As far as VS2010 variadic template support: I have no idea.
可变参数模板是一个黑客拼凑而成的补丁——你不会喜欢这个。做到这一点的方法(我突然想到)是将模板专门化与继承结合使用。沿着这些思路:
我以前从未这样做过,也没有测试过,但这是总体思路。您可以查看 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:
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.