c++ - 如何对内部模板类使用重新解释强制转换?

标签 c++ templates inner-classes reinterpret-cast

我有两个模板类:外部和内部。我正在从其他内部类对象进行类型转换到内部类。我收到编译错误。如何解决这个问题?

template<typename O>
struct outer
{
   template<typename I>
      struct inner
      {
      };

   inner<int> *ptr;
   outer();

};

   template<typename O,typename I>
void callme()
{
   reinterpret_cast< outer<O>::inner<I> *>(NULL);
}

最佳答案

你想要:

reinterpret_cast<typename outer<O>::template inner<I> *>(nullptr);
//               ^^^^^^^^           ^^^^^^^^             ^^^^^^^

名称 outerinner 是从属名称(它们依赖于模板参数),因此您需要显式指定它们的“种类”(值、类型、模板) )。

关于c++ - 如何对内部模板类使用重新解释强制转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43763109/

相关文章:

java - 为什么内部类的子类需要封闭实例?

c++ - 乘以 2 个相同/不同矩阵的性能

templates - 检查类型是否是模板的实例化

c++ - 在 C++ 中通过类型列表键入访问者

javascript - 使用模板但保留 jquery 绑定(bind)?

Java 内部类设计的好处

java - MVC 中通过线程从内部类调用外部类

c++ - 库 VS2015

c++ - 将 -1 转换为无符号类型

c++ - Linux Ubuntu下绕行功能