Templated Proxy design pattern

2019-09-22 07:48发布

问题:

I have a simple and reproducible code, which looks like so:

template <typename T>
class Proxy {
private:
    Wrap<T> &self; // If I comment this, it will work
public:
    Proxy() {}
};

template <typename T>
class Wrap {
    T *p;
public:
    Proxy<T> foo() {
        return Proxy<T>();
    }
};

int main()
{
    return 0;
}

The error I get is:

'Wrap' does not name a type

If I comment Wrap<T> &self, then it will work, but this is not what I need. I need Wrap<T> to be a member of Proxy class. How can I achieve this?

回答1:

You could add forward declaration of Wrap before the class definition of Proxy, otherwise the compiler can't know that it's the name of a class template. It's worth noting that reference data member doesn't require the type to be complete type, so forward declaration is enough here.

// forward declaration
template <typename T>
class Wrap;

template <typename T>
class Proxy {
private:
    Wrap<T> &self;
public:
    Proxy() {}
};

BTW the following issue would be that the reference data member self is not initialized.

LIVE of Clang

error: constructor for 'Proxy' must explicitly initialize the reference member 'self'



回答2:

You need to add a forward declaration:

template<class T> class Wrap;

template <typename T>
class Proxy {
private:
    Wrap<T> &self;
public:
    Proxy() {}
};

template <typename T>
class Wrap {
    T *p;
public:
    Proxy<T> foo() {
        return Proxy<T>();
    }
};

int main()
{
    return 0;
}

The code is just copied from the OP's question. But you still need to pay a attention to member Wrap<T> &self. As a reference, it should be initialized in constructor.