我正在尝试为头类的参考/文档创建一个更干净的头文件,同时仍然认识到模板的便利性。所以我有一个快速的 .h / .hpp 文件
// mempool.h
namespace internal {
  template<typename T,template<class> class Allocator>
  class MemoryPool;
}
template<typename T,template<class> class Allocator = std::allocator>
class MemoryPool
{
  private: internal::MemoryPool<T,Allocator> *pimpl;
  public: MemoryPool();
  public: virtual ~MemoryPool();
  public: void  flush()        { return pimpl->flush();      }
  public: T    *find(T object) { return pimpl->find(object); }
  public: T    *pop()          { return pimpl->pop();        }
  public: void  push(T object) { return pimpl->push(object); }
};
干净整洁。然后是一个 .hpp 文件
// mempool.hpp
#include <memory>
#include <mutex>
namespace cext {
namespace memory {
#include "memorypool.h"
template<typename T,template<class> class Allocator>
MemoryPool<T,Allocator>::MemoryPool()
:pimpl(new internal::MemoryPool<T,Allocator>())
{
}
template<typename T,template<class> class Allocator>
MemoryPool<T,Allocator>::~MemoryPool()
{
  delete pimpl;
}
namespace internal {
template<typename T,template<class> class Allocator = std::allocator>
class MemoryPool
{
  private: std::mutex mtx;
  private: Allocator<T> alloc;
  public: MemoryPool()
  :alloc()
  {
  //alloc.allocate(256); 
  }  
  public: ~MemoryPool()
  {
  //alloc.deallocate();  
  }  
  public: void flush()
  {
    mtx.lock();
    puts("flush");
    mtx.unlock();
  }
  public: T *find(T object)
  {
    mtx.lock();
    puts("find");
    mtx.unlock();
    return nullptr;
  }
  public: T *pop()
  {
    mtx.lock();
    puts("pop");
    mtx.unlock();
    return nullptr;
  }
  public: void push(T object)
  {
    mtx.lock();
    puts("push");
    mtx.unlock();
  }
};
} // end internal
} // end cext
} // end memory
所以我不认为使用 pimpl 对我有任何帮助,因为用户需要#include .hpp 文件。如果我没记错的话,对实现的更改会导致链接器更新,因此不会提高编译速度。考虑到我是如何将 .h 文件包含在 .hpp 文件中的,我可能可以一起取消 pimpl。
问:我想知道是否有一种简洁的方法来显示模板的最小标头,就像我在上面在 .h 文件中所做的那样,但仍然可以获得一些编译器加速?如果还有其他可行的方法,我不会对 pimpl 进行设置。
-干杯