Tuesday 15 July 2014

c++ - Pimpl idiom implementation depending on a template function -


Please consider the code given below.

The template parameter is a handler class that will have to provide the function bar () .

I am using Pimpl idiom to hide the implementation details foo . The constructor was defined in the foo.cpp before being the template parameter and it was all good.

  /////////// // foo .h /////////// Class Fu {Public: Template & lt; Square handlers & gt; Fu (handler and handler); Zero some function (); Private: / ** Private implementation details. * / Strip Impulse; Const std :: unique_ptr & lt; Imple & gt; M_impl; }; Template & lt; Square handlers & gt; Foo :: Foo (Handler and Handler): m_impl {new Impl {handler}} {} /////////// // foo.cpp /////////// # Include "Foo.h" / ** brings Foo's personal implementation details to the fore. * / Struct Foo: Implements {Public: Imple (Handler and Handler): M_Handler (Handler) {} Some Other Functions Zero () {m_handler- & gt; times (); } Private: Handler & amp; M_handler; Friend class fu; }; Zero Foo: someFunction () {m_impl- & gt; Some other functions (); }  

Once I started the template parameter, then the constructor was inserted in foo.h , due to which the following compiler error occurs:

Incomplete type allocation 'Foo :: Impulse' .

I understand why I get an error but I can not think of any way around it and still can anyone help Pimple?

You can solve it with or without virtual working. The first option is somewhat easy: Use the virtual function for the handler

  class handlerbase {.... virtual zeros bar () = 0; ...}; Square Fu {... std :: unique_ptr & lt; Handlerbase & gt; Handler; ...};  

or for example

  class implementbase {... virtual zeroes some functions () = 0; } Square fu {... std :: unique_ptr  

I think these are relatively simple to implement. You can see that when you use handlerbase then you do not even need pimple idioms

Second The option method is to use:

  square fu {public: template  

The type of foo remains independent of the handler. Assignment in the constructor creates a custom example of some function templates that are capable of calling the actual handler type. A template on someFunctionInstance handler is a result of immediate, but because it is a stable task, its type is also independent of the handler.

Although this solution is difficult and includes reinterpret_cast, it is still completely safe


No comments:

Post a Comment