SOLUTION
juanchopanza was right: the issue was, I needed a template keyword before the call to the function template Derived
inside the class template.
FIXES/MODIFICATIONS
- added the template keyword to help the compiler
- because class template
pimpl<>
contains astd::unique_ptr<>
member, it needs a move constructor or you will not be able to return an instance from a function - I added convenience operators to
pimpl<>
so it looks more like astd::unique_ptr<>
from the user's perspective - fixed a goofy refactoring error in member misname from
m
tom_up
inpimpl<>
- eliminate non-pimpl code for clarity
WORKING CODE
template<typename T>
class pimpl
{
std::unique_ptr<T> m_up;
public:
pimpl() { }
pimpl( pimpl&& rhs ) : m_up( std::move( rhs.m_up )) { }
template<typename ...Args>
pimpl( Args&& ...args )
: m_up{ new T{ std::forward<Args>(args)... } } { }
template<typename D,typename ...Args>
static pimpl<T> Derived( Args&& ...args )
{
pimpl<T> x;
x.m_up.reset( new D{ std::forward<Args>(args)... } );
return x;
}
~pimpl() { }
T* operator->() { return m_up.get(); }
T& operator*() { return *m_up.get(); }
T const* operator->() const { return m_up.get(); }
T const& operator*() const { return *m_up.get(); }
};
template<typename T>
struct coord_t
{
};
template<typename T>
struct simp_t : public coord_t<T>
{
T m_x, m_y;
simp_t( T x, T y ) : m_x( x ), m_y( y ) { }
};
template<typename T>
struct dep_t : public coord_t<T>
{
using parent_t = coord_t<T>;
parent_t const& m_parent;
T m_offset;
dep_t( parent_t const& p, T offset ) : m_parent( p ), m_offset( offset )
{ }
};
template<typename T=int>
class coordinate_t
{
pimpl<coord_t<T>> m_impl;
using my_simp_t = simp_t<T>;
using my_dep_t = dep_t<T>;
using my_pimpl_t = pimpl<coord_t<T>>;
public:
coordinate_t( T x, T y ) :
m_impl( my_pimpl_t::template Derived<my_simp_t>( x, y ))
{
}
coordinate_t( coordinate_t<T> const& parent, T const& offset ) :
m_impl( my_pimpl_t::template Derived<my_dep_t>( *parent.m_impl, offset ))
{
}
~coordinate_t() { }
};
int main()
{
coordinate_t<> ts( 3, 5 );
coordinate_t<> ts_derived( ts, 9 ); // ts + { 9, 9 }
}