Boost container fails to compile with undefined (b

2019-05-26 18:12发布

问题:

The following code fails to compile in MSVStudio 2010 Express, and seems to be because the boost container declaration creates a (static?) instance of the contained type. Changing boost::ptr_list<TypeContained> to std::list<TypeContained *> causes it to compile successfully, but I like the the boost containers. Anyone have an idea how I can get around this? The error is error C2504: 'Proxy<TypeContainer,TypeContained>' : base class undefined

#include <string>
#include <boost/ptr_container/ptr_list.hpp>

template <typename TypeContainer, typename TypeContained>
class Proxy
{
private:
    typename boost::ptr_list<TypeContained>::iterator m_clsPosition;

public:
    class Container {};
};

template <typename V> class Container;

template <typename V>
class Dependent : public Proxy<Container<V>, Dependent<V> >,
                  public V {};

template <typename V>
class Container : public Proxy<Container<V>, Dependent<V> >::Container {};

int main(int argc, char * argv[])
{
    Container<std::string> clsContainer;
    return 0;
}

回答1:

so, compiling with clang, the problem lies in boost trying to use sizeof on TypeContainer, but at this point, it's not yet determined what the size of TypeContainer is, since you're still defining it.

So, the root problem, as a simpler case:

template <typename A>
class T {
    static const int a = sizeof(T<A>);
};

int main() {
    T<int> d;
}

In other words, there's a loop dependency created by invoking sizeof, but using a pointer (which has a known size), this dependency is never created.