如果是模板类,是否可以隐藏库依赖项?

Is it possible to hide a library dependency if it's a template class?

本文关键字:隐藏 依赖 是否 如果      更新时间:2023-10-16

我有一个使用库B的库A。我希望A的用户使用B库中的模板类。但是A和B有不同的命名空间,是否可以在名称空间中封装/隐藏该类?

我尝试使用pimpl,但是...这是一个模板,所以不知道该如何做。

用于参考我想封装在库中的类是:

namespace anax {
template <typename T>
class Component : public BaseComponent
{
public:
    static detail::TypeId GetTypeId()
    {
        return detail::ClassTypeId<BaseComponent>::GetTypeId<T>();
    }
};
}

因此,A的用户必须这样做:

   class Position: public anax::Component<Position>
   {
       float x,y,z;
   }

我确切地问的是,是否可以将此anax :: component类封装/隐藏我的名称空间中:

   class Position: public myAnamespace::Component<Position>
   {
       float x,y,z;
   }

怎么样:

namespace myAnamespace{
    template<class Position>
    using Component = anax::Component<Position>;
}

或,在pre-c 11中,

namespace myAnamespace{
    template<class Position>
    struct Component
    {
         typedef anax::Component<Position> type;
    };
}
namespace anax
{
  class Component
  {
  };
};
namespace b
{
  using namespace anax;
  class Position : public Component
  {
  };
};