对模板化基类的模板化成员的调用失败
call to templated member of templated base class fails
我有一个问题,这里似乎已经讨论过了:CPP模板成员函数专业化
但是this->template
的解决方案与我的示例不兼容。
以下代码失败:
错误:类型"<未解析的重载函数类型>'和'int'到二进制"operator<"
带有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
。如果标识符后面有模板参数,之前有.
、->
或::
,并且它是依赖于模板参数的某个成员,则需要有template
关键字来消除小于号的歧义。
int nextState = this->template SM< StateBase, States...>::template GetNextState <1>( 1 );
差不多了,你需要另一个template
int nextState = this->template SM< StateBase, States...>::template GetNextState <1>( 1 );
~~~~~~~~
问题是,由于GetNextState
来自模板参数,它不知道它是静态变量、函数、模板函数还是其他什么。解析器需要继续,因此它假设它不是一个模板函数,因此<
被解析为小于运算符,而不是模板参数列表的开头。从那里,解析器会变得混乱,您会得到关于>
的无效操作数的错误。
相关文章:
- 为什么使用 P/Invoke 调用 dll 时,某些计算机中的 LoadLibrary 失败?
- 在WSL:configure_file上对config_file的每次调用都失败:配置文件时出现问题
- Active Directory:从网络服务帐户下运行的Windows服务调用ADsOpenObject时失败
- 从类型std::函数传递变量失败,尽管调用方期望的类型完全相同
- join() 失败,如果在线程内部调用 io_context.run()
- Netbeans 10:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- 为什么此对箭头 (->) 运算符的调用失败?
- 链接器命令失败,macOS 上的退出代码为 1(使用 -v 查看调用)
- 无法调用成员函数,尝试正确执行此操作仍然失败
- 非静态成员失败的线程调用函数
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 如何修复 clang: 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- 使用显式模板参数列表和 [temp.arg.explicit]/3 的函数调用的演绎失败
- G++ 内联在调用always_inline "int _rdrand16_step()"时失败
- Boost Beast 异步服务器失败,断言失败:(id_ != T::id) 在多个 aync 调用中
- 从 C#-DLL 调用函数的 C++ 失败
- Xcode:链接器命令失败,退出代码为 1(使用 -v 查看调用)[C++]
- 为什么在谷歌测试中调用"mkdtemp()"时失败
- 为什么 std::get<T> 其中 T 是调用 constexpr 函数失败的结果?
- OpenCV c++ 断言失败调用绘制轮廓