![](/img/trans.png)
[英]How to: Extend C++14 template function to variadic template, arguments
[英]c++14 variadic template issue 'is ambiguous'
我有这样的片段:
template<typename Last>
bool apply_impl(data_t * d) const
{
return this->Last::apply(*this, vs);
}
template<typename Head, typename ...Tail>
bool apply_impl(data_t * d) const
{
return this->Head::apply(*this, d) && this->template apply_impl<Tail...>(d);
}
编译器错误是:
error: call to member function 'apply_impl' is ambiguous
return this->Head::apply(*this, vs) && this->template apply_impl<Tail...>(vs);
怎么解决这个?
您应该能够使用标签分配来解决此问题以标记结束条件:
template <class...>
struct TypeList {};
template<typename Head, typename ...Tail>
bool apply_impl(data_t * d, TypeList<Head, Tail...> = {}) const
{
return this->Head::apply(*this, d) && this->apply_impl(d, TypeList<Tail...>{});
}
bool apply_impl(data_t * d, TypleList<>) const
{ return true; }
这样,模板版本将处理所有模板参数,非模板将仅提供终结符。
怎么解决这个?
template <typename Last>
bool apply_impl(data_t* d) const
{
return Last::apply(*this, vs);
}
template <typename Head, typename RunnerUp, typename... Tail>
// ~~~~~~~~~~~~~~~~^
bool apply_impl(data_t* d) const
{
return Head::apply(*this, d) && apply_impl<RunnerUp, Tail...>(d);
// ~~~~~~~^
}
在C ++ 17中:
template <typename Head, typename... Tail>
bool apply_impl(data_t* d) const
{
return Head::apply(*this, d) && (Tail::apply(*this, d) && ...);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.