【问题标题】:Boost container fails to compile with undefined (but declared) classBoost 容器无法使用未定义(但已声明)的类进行编译
【发布时间】:2012-06-24 02:35:51
【问题描述】:

以下代码无法在 MSVStudio 2010 Express 中编译,似乎是因为 boost 容器声明创建了包含类型的(静态?)实例。将 boost::ptr_list<TypeContained> 更改为 std::list<TypeContained *> 会使其编译成功,但我喜欢 boost 容器。任何人都知道我该如何解决这个问题?错误是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;
}

【问题讨论】:

  • 你的编译器选项是什么?

标签: c++ templates boost forward-declaration


【解决方案1】:

所以,用 clang 编译,问题在于 boost 试图在 TypeContainer 上使用 sizeof,但此时,尚未确定 TypeContainer 的大小,因为您仍在定义它。

所以,根本问题,作为一个更简单的情况:

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

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

换句话说,通过调用 sizeof 创建了一个循环依赖项,但使用指针(具有已知大小),永远不会创建此依赖项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-15
    • 2020-07-23
    • 2020-10-03
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    相关资源
    最近更新 更多