关于一个奇怪的template的使用疑问

C++语言 码拜 9年前 (2015-10-09) 1016次浏览
   //! \brief Compute the distance to a finite line
    struct MyPointFiniteLineDistanceFunctor {
        template <class _LineT, class _PointContainerT, class _Vec3Derived>
        static inline typename _LineT::Scalar eval(
                  _PointContainerT const& extrema0
                , _LineT const& l0
                , _Vec3Derived const& q
                ) /*const*/ {
            typedef typename _PointContainerT::value_type PointT;
            typedef typename _LineT::Scalar Scalar;

            // The algorithm is very similar to the one used in DecideMergeLineFunctor::eval,
            // except that we need to compute the distance between the line l0 and q (and not to l1 extents)
            // There are two cases:
            //   – q is projected orthogonally to the finite line l0, in that case we return the orthogonal direction
            //   – q is projected out of the line, so we return the minimum distance to the two extents

            // check if q is in the band aligned with l0, thickness=scale
            const PointT & l0a = extrema0[0];
            const PointT & l0b = extrema0[1];

            // Get line direction using extents
            const PointT l0dir = (l0b – l0a).normalized();
            // Compute dq, the parametric position of q on the line
            const Scalar dq   = l0dir.dot(q-l0a);

            // if orthogonally projected onto the finite line
            if ((dq >= Scalar(0.) && dq <= (l0b – l0a).norm())){
                return std::abs(l0.template normal().dot(q-l0.pos())); // orthogonal direction to the line
            }
return (q – l0a).norm() < (q – l0b).norm() ? (q – l0a).norm() : (q – l0b).norm();
       }
    };

这是下载的别人的代码,用来计算一个点q到一条线段l0的距离。
如果点向线段所在直线投影在线段上:return std::abs(l0.template normal().dot(q-l0.pos())); // orthogonal direction to the line
否则:return (q – l0a).norm() < (q – l0b).norm() ? (q – l0a).norm() : (q – l0b).norm();

这个工程中都是使用类型模板、类模板来编程的。
问题是:第一个return中的l0.template normal()中的template是什么用法?
在这个工程中有这样的用法,我翻阅了很多template的用法,都没看到过这个。

会报语法错误:“template”
注释掉就可以过
但是另外一个
std::for_each(extrema0.begin(), extrema0.end(), [&points] (const PointT &p){ points.push_back(p.template head<2>()); });
却没有报错。

解决方案:40分
4 When the name of a member template specialization appears after . or -> in a postfix-expression or after a
nested-name-specifier in a qualified-id, and the object expression of the postfix-expression is type-dependent
or the nested-name-specifier in the qualified-id refers to a dependent type, but the name is not a member of
the current instantiation (14.6.2.1), the member template name must be prefixed by the keyword template.
Otherwise the name is assumed to name a non-template. [ Example:

struct X {
    template<std::size_t> X* alloc();
    template<std::size_t> static X* adjust();
};
template<class T> void f(T* p) {
    T* p1 = p->alloc<200>(); // ill-formed: < means less than
    T* p2 = p->template alloc<200>(); // OK: < starts template argument list
    T::adjust<100>(); // ill-formed: < means less than
    T::template adjust<100>(); // OK: < starts template argument list
}

— end example ]


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于一个奇怪的template的使用疑问
喜欢 (0)
[1034331897@qq.com]
分享 (0)