gpt4 book ai didi

c++14可变参数模板问题 'is ambiguous'

转载 作者:行者123 更新时间:2023-11-30 02:26:20 31 4
gpt4 key购买 nike

我有这样的片段:

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

如何解决?

最佳答案

您应该能够使用标记分派(dispatch)来标记结束条件来解决此问题:

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

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

关于c++14可变参数模板问题 'is ambiguous',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42929840/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com