【问题标题】:Forward declaring a template's member typedef前向声明模板的成员 typedef
【发布时间】:2018-02-19 17:08:17
【问题描述】:

我使用了一个库(我无法修改)并声明了一个包含 Ptr typedef

PointCloud 模板
namespace pcl
{
    template <typename PointT>
    class PCL_EXPORTS PointCloud
    {
        ...
        typedef boost::shared_ptr<PointCloud<PointT> > Ptr;
    }
}

现在,我需要转发声明 Ptr,但我不知道该怎么做。我已经完成了

namespace pcl
{
    class PointXYZ;
    template<class pointT> class PointCloud;
}

但我被困在这里,无论我做什么,我似乎都无法转发声明 Ptr typedef

有什么想法吗?

---------编辑---------

我需要这个前向声明的原因是我需要将一个函数声明到我自己的头文件中。我希望这个函数将 PointCloud::Ptr 作为参数,因为我想要输入函数的 PointCloud 存储在 点云::Ptr.

【问题讨论】:

  • 你不能这样做。
  • 您要解决的实际问题是什么?用你自己的库类及其成员的声明来破解几乎总是不是正确的答案。
  • 然后在你的函数中使用boost::shared_ptr&lt;PointCloud&lt;PointXYZ&gt; &gt;?或者您可以将函数设为模板,并将boost::shared_ptr&lt;PointCloud&lt;T&gt; &gt; 用于任何类型的T
  • 通常的做法是把你的 header 指向#include 的 header 有PointCloud 的定义,然后只声明函数接受PointCloud&lt;PointXYZ&gt;::Ptr
  • 您可以只转发声明类型,如果您愿意,可以创建您自己的 typedef using LibFooPtr = boost::shared_ptr&lt;LibFooType&gt;; ?您的类型 def 不需要与库类型 def 相同。

标签: c++ c++11


【解决方案1】:

Typedef 是类型的别名。如果有意义的话,它们本身不是类型。

namespace pcl
{
  class PointXYZ;
  template<class pointT> class PointCloud;
}
template<class T>
using foo = boost::shared_ptr<pcl::PointCloud<T>>;

foo&lt;X&gt;pcl::PointCloud&lt;T&gt;::Ptr 的类型相同。

【讨论】:

    猜你喜欢
    • 2021-10-08
    • 2014-04-22
    • 1970-01-01
    • 2016-06-19
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    相关资源
    最近更新 更多