如何将模板作为模板参数传递给模板? [英] How do I pass a template as a template parameter to a template?
本文介绍了如何将模板作为模板参数传递给模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想写如下:
// I don't know how this particular syntax should look...
template<typename template<typename Ty> FunctorT>
Something MergeSomething(const Something& lhs, const Something& rhs)
{
Something result(lhs);
if (lhs.IsUnsigned() && rhs.IsUnsigned())
{
result.SetUnsigned(FunctorT<unsigned __int64>()(lhs.UnsignedValue(), rhs.UnsignedValue()));
}
else
{
result.SetSigned(FunctorT<__int64>()(lhs.SignedValue(), rhs.SignedValue()));
}
return result;
}
这将像下面这样使用:
Something a, b;
Something c = MergeSomething<std::plus>(a, b);
如何实现?
推荐答案
这只是一个模板模板参数。语法非常接近你想象的。这是:
This is just a "template template argument". The syntax is very close to what you imagined. Here it is:
template< template<typename Ty> class FunctorT>
Something MergeSomething(const Something& lhs, const Something& rhs)
{
Something result(lhs);
if (lhs.IsUnsigned() && rhs.IsUnsigned())
{
result.SetUnsigned(FunctorT<unsigned __int64>()(lhs.UnsignedValue(), rhs.UnsignedValue()));
}
else
{
result.SetSigned(FunctorT<__int64>()(lhs.SignedValue(), rhs.SignedValue()));
}
return result;
}
您的使用案例应该像您发布的那样工作。
Your use-case should work like you posted it.
这篇关于如何将模板作为模板参数传递给模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文