Please consider the code below.
The template parameter is a handler class that must provide the function bar()
.
I'm using the Pimpl idiom to hide the implementation details of Foo
. Prior to having a template parameter the constructor definition was in foo.cpp
and all was good.
///////////
// foo.h
///////////
class Foo
{
public:
template <class Handler>
Foo(Handler& handler);
void someFunction();
private:
/** Private implementation details. */
struct Impl;
const std::unique_ptr<Impl> m_impl;
};
template <class Handler>
Foo::Foo(Handler& handler) : m_impl{new Impl{handler}}
{
}
///////////
// foo.cpp
///////////
#include "foo.h"
/** Encapsulates the private implementation details of Foo. */
struct Foo::Impl
{
public:
Impl(Handler& handler) : m_handler(handler)
{
}
void someOtherFunction()
{
m_handler->bar();
}
private:
Handler& m_handler;
friend class Foo;
};
void Foo::someFunction()
{
m_impl->someOtherFunction();
}
Once I introduced the template parameter I had to put the constructor in foo.h
, which causes the following compiler error:
Allocation of incomplete type 'Foo::Impl'
.
I understand the reason why I get the error but I can't think of a way around it and still use the Pimpl idiom.
Can anyone help?