2017-03-21 13 views
1

私は、このようなスニペットを持っている: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); 

これを解決する方法は?

+0

[MCVE]を提供してください。 – Barry

+0

詳細については必要ないように見える、人々は私が必要なものを手に入れました:) – StNickolay

+0

@Barry何が足りないのですか? – Angew

答えて

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

解決方法C++ 17では

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); 
    //           ~~~~~~~^ 
} 

template <typename Head, typename... Tail> 
bool apply_impl(data_t* d) const 
{ 
    return Head::apply(*this, d) && (Tail::apply(*this, d) && ...); 
} 
関連する問題