Typedef inside template class

解释了在模板类内部使用typedef导致的问题,并提供了解决方法。

http://stackoverflow.com/questions/5524744/typedef-inside-template-class-doesnt-work

I have a problem with the following code:

template <typename U>
class lamePtr
{
public:
    typedef U* ptr;
};

template <typename U>
class smarterPointer
{
    public:
    void funFun()
    {
        typedef lamePtr<U> someType;
        someType::ptr query;
    }
};

As you see, I have a typedef inside lamePtr. Inside smarterPointer class I have a function funFun(). What am I trying to do is to make another typedef someType. Till that line, everything works fine until we get to the line with someType::ptr query.

What I want here to happen is that "query" will become lamePtr< U >::ptr (a simple value, not a typedef ;). However, I get compilation errors (with gcc 4.4.3):

temp.cpp: In member function void smarterPointer<U>::funFun()’:
temp.cpp:15: error: expected ‘;’ before query

What am I doing wrong here?



someType, as lamePtr<U> is a "dependant name". It depends on what U is as to whether or not there is a member ptr and, if so, what kind of "thing" that member is.

Of course, you know that for all TlamePtr<T>::ptr is a type, but at this stage of compilation the parser does not know that.

Use the typename keyword to hint to the parser that it's a type. The rest will be resolved later in the compilation process. Just a little C++ quirk.

template <typename U>
class lamePtr
{
public:
    typedef U* ptr;
};

template <typename U>
class smarterPointer
{
    public:
    void funFun()
    {
        typedef lamePtr<U> someType;
        typename someType::ptr query;
    }
};


总结就是:

在父类中有一个类型叫ptr, 但是对于子类而言,如果用lamePtr<U>::ptr的形式,不能判断是静态成员变量还是类型,所以要用typename标记出这是类型,而不是成员变量

/** * @file polygon.h * @brief Polygon class */ #ifndef DECOMP_POLYGON_H #define DECOMP_POLYGON_H #include <decomp_basis/data_type.h> ///Hyperplane class template <int Dim> struct Hyperplane { Hyperplane() {} Hyperplane(const Vecf<Dim>& p, const Vecf<Dim>& n) : p_(p), n_(n) {} /// Calculate the signed distance from point decimal_t signed_dist(const Vecf<Dim>& pt) const { return n_.dot(pt - p_); } /// Calculate the distance from point decimal_t dist(const Vecf<Dim>& pt) const { return std::abs(signed_dist(pt)); } /// Point on the plane Vecf<Dim> p_; /// Normal of the plane, directional Vecf<Dim> n_; }; ///Hyperplane2D: first is the point on the hyperplane, second is the normal typedef Hyperplane<2> Hyperplane2D; ///Hyperplane3D: first is the point on the hyperplane, second is the normal typedef Hyperplane<3> Hyperplane3D; ///Polyhedron class template <int Dim> struct Polyhedron { ///Null constructor Polyhedron() {} ///Construct from Hyperplane array Polyhedron(const vec_E<Hyperplane<Dim>>& vs) : vs_(vs) {} ///Append Hyperplane void add(const Hyperplane<Dim>& v) { vs_.push_back(v); } /// Check if the point is inside polyhedron, non-exclusive bool inside(const Vecf<Dim>& pt) const { for (const auto& v : vs_) { if (v.signed_dist(pt) > epsilon_) { //printf("rejected pt: (%f, %f), d: %f\n",pt(0), pt(1), v.signed_dist(pt)); return false; } } return true; } /// Calculate points inside polyhedron, non-exclusive vec_Vecf<Dim> points_inside(const vec_Vecf<Dim> &O) const { vec_Vecf<Dim> new_O; for (const auto &it : O) { if (inside(it)) new_O.push_back(it); } return new_O; } /// Calculate normals, used for visualization vec_E<std::pair<Vecf<Dim>, Vecf<Dim>>> cal_normals() const { vec_E<std::pair<Vecf<Dim>, Vecf<Dim>>> ns(vs_.size()); for (size_t i = 0; i < vs_.size(); i++) ns[i] = std::make_pair(vs_[i].p_, vs_[i].n_); // fist is point, second is normal return ns; } /// Get the hyperplane array vec_E<Hyperplane<Dim>> hyperplanes() const { return vs_; } /// Hyperplane array vec_E<Hyperplane<Dim>> vs_; // normal must go outside }; ///Polyhedron2D, consists of 2D hyperplane typedef Polyhedron<2> Polyhedron2D; ///Polyhedron3D, consists of 3D hyperplane typedef Polyhedron<3> Polyhedron3D; ///[A, b] for \f$Ax < b\f$ template <int Dim> struct LinearConstraint { ///Null constructor LinearConstraint() {} /// Construct from \f$A, b\f$ directly, s.t \f$Ax < b\f$ LinearConstraint(const MatDNf<Dim>& A, const VecDf& b) : A_(A), b_(b) {} /** * @brief Construct from a inside point and hyperplane array * @param p0 point that is inside * @param vs hyperplane array, normal should go outside */ LinearConstraint(const Vecf<Dim> p0, const vec_E<Hyperplane<Dim>>& vs) { const unsigned int size = vs.size(); MatDNf<Dim> A(size, Dim); VecDf b(size); for (unsigned int i = 0; i < size; i++) { auto n = vs[i].n_; decimal_t c = vs[i].p_.dot(n); if (n.dot(p0) - c > 0) { n = -n; c = -c; } A.row(i) = n; b(i) = c; } A_ = A; b_ = b; } /// Check if the point is inside polyhedron using linear constraint bool inside(const Vecf<Dim> &pt) { VecDf d = A_ * pt - b_; for (unsigned int i = 0; i < d.rows(); i++) { if (d(i) > 0) return false; } return true; } /// Get \f$A\f$ matrix MatDNf<Dim> A() const { return A_; } /// Get \f$b\f$ matrix VecDf b() const { return b_; } MatDNf<Dim> A_; VecDf b_; }; ///LinearConstraint 2D typedef LinearConstraint<2> LinearConstraint2D; ///LinearConstraint 3D typedef LinearConstraint<3> LinearConstraint3D; #endif 详细注释上述代码
最新发布
09-20
/** * @file ellipsoid.h * @brief Ellipsoid class */ #ifndef DECOMP_ELLIPSOID_H #define DECOMP_ELLIPSOID_H #include <iostream> #include <decomp_basis/data_type.h> #include <decomp_geometry/polyhedron.h> template <int Dim> struct Ellipsoid { Ellipsoid() {} Ellipsoid(const Matf<Dim, Dim>& C, const Vecf<Dim>& d) : C_(C), d_(d) {} /// Calculate distance to the center decimal_t dist(const Vecf<Dim>& pt) const { return (C_.inverse() * (pt - d_)).norm(); } /// Check if the point is inside, non-exclusive bool inside(const Vecf<Dim>& pt) const { return dist(pt) <= 1; } /// Calculate points inside ellipsoid, non-exclusive vec_Vecf<Dim> points_inside(const vec_Vecf<Dim> &O) const { vec_Vecf<Dim> new_O; for (const auto &it : O) { if (inside(it)) new_O.push_back(it); } return new_O; } ///Find the closest point Vecf<Dim> closest_point(const vec_Vecf<Dim> &O) const { Vecf<Dim> pt = Vecf<Dim>::Zero(); decimal_t min_dist = std::numeric_limits<decimal_t>::max(); for (const auto &it : O) { decimal_t d = dist(it); if (d < min_dist) { min_dist = d; pt = it; } } return pt; } ///Find the closest hyperplane from the closest point Hyperplane<Dim> closest_hyperplane(const vec_Vecf<Dim> &O) const { const auto closest_pt = closest_point(O); const auto n = C_.inverse() * C_.inverse().transpose() * (closest_pt - d_); return Hyperplane<Dim>(closest_pt, n.normalized()); } /// Sample n points along the contour template<int U = Dim> typename std::enable_if<U == 2, vec_Vecf<U>>::type sample(int num) const { vec_Vecf<Dim> pts; decimal_t dyaw = M_PI*2/num; for(decimal_t yaw = 0; yaw < M_PI*2; yaw+=dyaw) { Vecf<Dim> pt; pt << cos(yaw), sin(yaw); pts.push_back(C_ * pt + d_); } return pts; } void print() const { std::cout << "C: " << C_ << std::endl; std::cout << "d: " << d_ << std::endl; } /// Get ellipsoid volume decimal_t volume() const { return C_.determinant(); } /// Get C matrix Matf<Dim, Dim> C() const { return C_; } /// Get center Vecf<Dim> d() const { return d_; } Matf<Dim, Dim> C_; Vecf<Dim> d_; }; typedef Ellipsoid<2> Ellipsoid2D; typedef Ellipsoid<3> Ellipsoid3D; #endif 详细注释上述代码
09-20
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值