简体   繁体   English

模板基类的C ++调用模板方法

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

I have got the following scenario: 我有以下情况:

one variadic template class which inherits "itself" to resolve the variadic template parameter. 一个可变参数模板类,它继承“自身”来解析可变参数模板参数。 The variadic template class has two template methods. 可变参数模板类具有两个模板方法。 Within these methods i want to call the base template class template method. 在这些方法中,我想调用基础模板类的模板方法。 For the set method it seems to work. 对于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";
        }
};

How can i call the base class's template function? 如何调用基类的模板函数?

通常依赖名称的问题:如ValueProvider<R...>要看R你必须消除歧义是get在该范围内是一个模板:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM