Question

BACKGROUND

I have two implementations of coord_t

  1. simp_t that simply stores x,y
  2. dep_t which takes a dependent parent coord_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 }
}
Was it helpful?

Solution

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

  1. added the template keyword to help the compiler
  2. because class template pimpl<> contains a std::unique_ptr<> member, it needs a move constructor or you will not be able to return an instance from a function
  3. I added convenience operators to pimpl<> so it looks more like a std::unique_ptr<> from the user's perspective
  4. fixed a goofy refactoring error in member misname from m to m_up in pimpl<>
  5. 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 }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top