pimpl for a templated class

前端 未结 3 787
攒了一身酷
攒了一身酷 2020-12-30 03:01

I want to use the pimpl idiom to avoid having users of my library need our external dependencies (like boost, etc) however when my class is templated that seems to be imposs

相关标签:
3条回答
  • 2020-12-30 03:33

    There are two general solutions:

    • while the interface depends on some type T, it defers to a more weakly typed implementation (e.g. one using void* pointers directly or trough type erasure), or

    • you support only a specific and quite limited number of types.

    The second solution is relevant for e.g. char/wchar_t-dependent stuff.

    The first solution was quite common in the early days of C++ templates, because at that time compilers were not good at recognizing commonalities in the generated machine code, and would introduce so called “code bloat”. Today, much to the surprise of any novice who tries it out, a templated solution can often have smaller machine code footprint than a solution relying on runtime polymorphism. Of course, YMMV.

    Cheers & hth.,

    0 讨论(0)
  • 2020-12-30 03:39

    If the class is templated, your users essentially need to compile it (and this is literally true in the most widely-used C++ implementations) and so they need your external dependencies.

    The simplest solution is to put the bulk of your class's implementation in a non-template base class (or encapsulated member object of some class). Solve the module-hiding problem there.

    And then write the template derived (or enclosing) class to add type safety to it.

    For example, suppose you have a template that provides the amazing ability to allocate on first access (omitting the necessary copy constructor, assignment, destructor):

    template <class T>
    class MyContainer
    {
        T *instance_;
    
    public:
        MyContainer() : instance_(0) {}
    
        T &access()
        {
            if (instance_ == 0)
                instance_ = new T();
    
            return *instance_;
        }
    };
    

    If you wanted the "logic" to be separated into a non-template base class, you'd have to parameterise the behaviour in the non-template way, which is to say, use virtual functions:

    class MyBase
    {
        void *instance_;
    
        virtual void *allocate() = 0;
    
    public:
        MyBase() : instance_(0) {}
    
        void *access()
        {
            if (instance_ == 0)
                instance_ = allocate();
    
            return instance_;
        }
    };
    

    Then you can add the type-awareness in the outer layer:

    template <class T>
    class MyContainer : MyBase
    {
        virtual void *allocate()
            { return new T(); }
    
    public:
        T &access()
            { return *(reinterpret_cast<T *>(MyBase::access())); }
    };
    

    i.e. You use virtual functions to allow the template to "fill in" the type-dependent operations. Obviously this pattern would only really make sense if you have some business logic that is worth the effort of hiding.

    0 讨论(0)
  • 2020-12-30 03:42

    You can explicitly instantiate templates in the source file, but that is possible only if you know what the template type is going to be. Otherwise, do not use pimpl idiom for templates.

    Something like this :

    header.hpp :

    #ifndef HEADER_HPP
    #define HEADER_HPP
    
    template< typename T >
    class A
    {
      // constructor+methods + pimpl
    };
    
    #endif
    

    source.cpp :

    #include "header.hpp"
    
    // implementation
    
    // explicitly instantiate for types that will be used
    template class A< int >;
    template class A< float >;
    // etc...
    
    0 讨论(0)
提交回复
热议问题