c++ - 调用模板化基类的模板化成员失败

标签 c++ templates c++11

<分区>

我有一个问题似乎已经在这里讨论过: CPP templated member function specialization

但是使用this->template 的解决方案不适用于我的示例。

以下代码失败:

error: invalid operands of types '<unresolved overloaded function type>' and 'int'
       to binary 'operator<'

with gcc 4.8.1

class Base { public: virtual int Do(){return 0;} };
class State1: public Base {};
class State2: public Base {};

template <typename ... T> class SM;

template <class StateBase, class HeadState, class ... States >
class SM<StateBase, HeadState, States...> : public SM< StateBase, States...>
{
    protected:
        HeadState headState;
        template<int cnt> StateBase* GetNextState ( unsigned int index ) { return headState; }
};  

template <class StateBase, class HeadState>
class SM< StateBase, HeadState>
{
    protected:
        HeadState headState;
        template<int cnt> StateBase* GetNextState ( unsigned int index ) { return headState; }
};  

template <class StateBase, class ... States >
class TopSM: public SM< StateBase, States...>
{
    public:
        void DoIt()
        {
            // following code fails with 
            // error: invalid operands of types '<unresolved overloaded function type>' and 'int' to binary 'operator<'
            int nextState = this->template SM< StateBase, States...>::GetNextState <1>( 1 );
        }
};  

TopSM<Base, State1, State2> sm;

int main()
{
    sm.DoIt();
    return 0;
}

最佳答案

GetNextState 之前您需要另一个模板。如果在标识符和 .->:: 之后有模板参数,并且它是依赖于模板参数,需要有一个 template 关键字来消除小于号的歧义。

int nextState = this->template SM< StateBase, States...>::template GetNextState <1>( 1 );

关于c++ - 调用模板化基类的模板化成员失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18098495/

相关文章:

c++ - 为什么在 std::aligned_storage 中定义扩展对齐实现

c++ - 函数指针和类模板

有和没有主体的 C++ 部分构造函数表现不同

c++ - 比较不同符号的整数时发出警告

c++ - 处理 WM_KEYDOWN

c++ - 无法在 C++ 中定义++ 运算符,这里有什么问题?

c++ - 链内是什么意思?

c++ - 有关模板和虚拟功能的问题

c++ - 从嵌套模板中删除 const

c++ - shared_ptr 问题从成员函数返回向上转换版本