调用“this->template \[somename\]”有什么作用?

新手上路,请多包涵

我已经搜索了这个问题,但我找不到任何东西。有没有更好的方法在 Google 中查询类似的内容,或者任何人都可以提供一个或多个链接或相当详细的解释?谢谢!

编辑:这是一个例子

template< typename T, size_t N>
struct Vector {
public:
   Vector() {
       this->template operator=(0);
   }

   // ...

   template< typename U >
   typename boost::enable_if< boost::is_convertible< U, T >, Vector& >::type operator=(Vector< U, N > const & other) {
       typename Vector< U, N >::ConstIterator j = other.begin();
       for (Iterator i = begin(); i != end(); ++i, ++j)
           (*i) = (*j);
       return *this;
   }
};

此示例来自 Google Code 上的 ndarray 项目,不是我自己的代码。

原文由 Phillip Cloud 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 337
1 个回答

这是一个需要 this->template 的示例。不过,它与 OP 的示例并不真正匹配:

 #include <iostream>

template <class T>
struct X
{
    template <unsigned N>
        void alloc() {std::cout << "alloc<" << N << ">()\n";}
};

template <class T>
struct Y
    : public X<T>
{
    void test()
    {
        this->template alloc<200>();
    }
};

int main()
{
    Y<int> y;
    y.test();
}

在此示例中,需要 this 否则 alloc 不会在基类中查找,因为基类依赖于模板参数 Ttemplate 是必需的,否则用于打开包含 200 的模板参数列表的“<”将指示小于号 ([temp.names]/4)。

原文由 Howard Hinnant 发布,翻译遵循 CC BY-SA 2.5 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题