BACKGROUND
I have two implementations of coord_t
simp_t
that simply stores x,ydep_t
which takes a dependent parentcoord_t
and adds an offset to it
These are lower-level implementation classes. At the user level, the usage should look like this:
coordinate_t<> ts( 3, 5 );
coordinate_t<> ts_derived( ts, 9 ); // ts + { 9, 9 }
PROBLEM
I have working code if I implement the underlying using std::unique_ptr<>
. However, when I try to convert the implementation to pimpl<>
, I get the following compile errors on g++ (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3
:
junk.cpp: In constructor ‘coordinate_t<T>::coordinate_t(T, T)’:
junk.cpp:54:47: error: expected primary-expression before ‘>’ token
junk.cpp: In constructor ‘coordinate_t<T>::coordinate_t(const coordinate_t<T>&, const T&)’:
junk.cpp:58:46: error: expected primary-expression before ‘>’ token
What am I doing wrong and how do I fix this to build correctly?
CODE
The code, as-is, fails to compile, but if you comment out #define USE_PIMPL
to use std::unique_ptr<>
directly, instead, it compiles fine.
#include <memory>
template<typename T>
class pimpl
{
std::unique_ptr<T> m_up;
public:
pimpl() { }
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.reset( new D{ std::forward<Args>(args)... } );
return x;
}
~pimpl() { }
};
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 )
{ }
};
#define USE_PIMPL // if we comment this out and USE_UNIQUE_PTR, it works ok
#ifdef USE_PIMPL
template<typename T=int>
class coordinate_t
{
pimpl<coord_t<T>> m_impl;
public:
coordinate_t( T x, T y ) :
m_impl( pimpl<coord_t<T>>::Derived<simp_t<T>>( x, y )) // ERROR HERE
{
}
coordinate_t( coordinate_t<T> const& parent, T const& offset ) :
m_impl( pimpl<coord_t<T>>::Derived<dep_t<T>>( parent, offset )) // ERROR HERE
{
}
~coordinate_t() { }
};
#else
template<typename T=int>
class coordinate_t
{
std::unique_ptr<coord_t<T>> m_impl;
public:
coordinate_t( T x, T y ) :
m_impl{ new simp_t<T>( x, y ) }
{
}
coordinate_t( coordinate_t<T> const& parent, T const& offset ) :
m_impl{ new dep_t<T>( *parent.m_impl, offset ) }
{
}
~coordinate_t() { }
};
#endif
int main()
{
coordinate_t<> ts( 3, 5 );
coordinate_t<> ts_derived( ts, 9 ); // ts + { 9, 9 }
}
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
pimpl<>
contains astd::unique_ptr<>
member, it needs a move constructor or you will not be able to return an instance from a functionpimpl<>
so it looks more like astd::unique_ptr<>
from the user's perspectivem
tom_up
inpimpl<>
WORKING CODE