My colleague insists our project should be able to use a template like this:
template <typename T, typename B>
class Foo
{
public:
Foo(T t, B b);
// some more functions
private:
struct pimpl;
std::unique_ptr<pimpl> m_impl;
};
I'm not sure if they're just trying to test me or what, but I can't figure what's the proper and correct way we should have to allocate m_impl
. They insist it should be possible to write the Foo::Foo(T t, B b)
constructor with this.
How should I be able to allocate it:
template<typename T, typename B>
Foo<T, Op>::Foo(T t, B b)
{
m_impl = // ... what goes here ?
}
To me, it seems anything I do will be a hack since the class is private and not defined. I tried:
m_impl = std::unique_ptr<pimpl>(reinterpret_cast<pimpl*>(new T));
But this results in
error: implicit instantiation of undefined member 'accumulator<int, std::plus>::pimpl'
from
in instantiation of member function 'std::unique_ptr<Foo<int, std::plus>::pimpl>::operator=' requested here pimpl_ = std::unique_ptr(reinterpret_cast<pimpl*>(new T));
Is there an elegant way to go with such a pimpl in a templated class ?