ABu
ABu

Reputation: 12299

Pointer to implementation pattern

I'm trying to implement the pointer to implementation pattern with template classes, and, in order to make independent class and its implementation, I use the next approach:

template<typename T>
struct __A_impl;

template<typename T>
struct A
{
    using __impl_t = __A_impl<T>;

    A(__impl_t* t);

    __impl_t* _t;
};

template<typename T>
struct B
{
    T _t;
};

template<typename T>
using __A_impl = B<T>;

With this form, if I want to change the name of 'B', this doens't affect in any way the definition of A. But, I receive the next error with gcc:

test.cpp:21:22: error: declaration of template 'template<class T> using __A_impl =    B<T>'
test.cpp:2:7: error: conflicts with previous declaration 'template<class T> class __A_impl'
test.cpp:21:22: error: redeclaration of 'template<class T> using __A_impl = B<T>'
test.cpp:2:7: note: previous declaration 'template<class T> class __A_impl'

How can I achieve this? Because with a typedef declarator isn't possible.

Upvotes: 0

Views: 925

Answers (2)

Puppy
Puppy

Reputation: 146968

You cannot forward declare a typedef. You can only forward declare classes/structs/enums.

Also, PIMPL is usually used to prevent the compiler from seeing the implementation, but in your case that's never going to happen because it's a template, so I don't see the point.

Upvotes: 2

Pubby
Pubby

Reputation: 53067

Well, this compiles, although it might not be what you want:

template<typename T>
struct B
{
    T _t;
};

template<typename T>
using __A_impl = B<T>;

template<typename T>
struct A
{
    using __impl_t = __A_impl<T>;

    A(__impl_t* t);

    __impl_t* _t;
};

Anyway, two things:

  1. Why not just use std::unique_ptr?
  2. Double underscores are reserved for implementation. Don't use them.

Upvotes: 0

Related Questions