簡體   English   中英

模板基類的C ++調用模板方法

[英]C++ calling template method of template base class

我有以下情況:

一個可變參數模板類,它繼承“自身”來解析可變參數模板參數。 可變參數模板類具有兩個模板方法。 在這些方法中,我想調用基礎模板類的模板方法。 對於set方法,它似乎起作用。

template<class T, class ... R>
class ValueProvider : ValueProvider<R...>{
    public:

        T value;
        const std::type_info& type = typeid(T);

        ValueProvider()
        {}

        template<class G>
        inline G get(){
            if(this->type.hash_code() == typeid(G).hash_code())
                return this->value;
            else{
                //----- interesting part -----
                return ValueProvider<R...>::get<G>();        /*<- compile ERROR: expected primary-expression before ‘>’ token*/
                return ValueProvider<R...>::get();        /*<- compile ERROR: no matching function for call to ‘ValueProvider<int, char>::get()’*/
                return ValueProvider::get<G>();        /*<- runtime ERROR (infinit recursion)*/
            }
        }

        template<class G>
        void set(G p){
            if(this->type.hash_code() == typeid(G).hash_code())
                this->value = p;
            else
                ValueProvider<R...>::set(p);
        }
};

template<class T>
class ValueProvider<T>{
    public:
        T value;
        const std::type_info& type = typeid(T);

        ValueProvider()
        {}

        template<class G>
        inline G get(){
            if(this->type.hash_code() == typeid(G).hash_code())
                return this->value;
            throw "fail";
        }

        template<class G>
        void set(G p){
            if(this->type.hash_code() == typeid(G).hash_code())
                this->value = p;
            else
                throw "fail";
        }
};

如何調用基類的模板函數?

通常依賴名稱的問題:如ValueProvider<R...>要看R你必須消除歧義是get在該范圍內是一個模板:

return ValueProvider<R...>::template get<G>();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM