c ++ 14可变参数模板问题'模棱两可'

时间:2017-03-21 14:24:07

标签: c++ c++14

我有这样的片段:

    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);

如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

您应该能够使用标签调度来解决此问题,以标记结束条件:

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; }

这样,模板版本将处理所有模板参数,非模板将只提供终结符。

答案 1 :(得分:1)

  

如何解决此问题?

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) && ...);
}