c++ - 模板类的内部类不会在 XCode 4.5.2 上编译

标签 c++ xcode templates stl iterator

在 XCode 4.5.2 中类 Proxy如下定义不编译。如有必要,我可以提供有关特定编译器的更多详细信息,尽管它应该是默认的,因为我没有更改 XCode 配置中的任何内容。它在 VStudio Express 中编译。

#include <list>
#include <boost/thread/tss.hpp>

template <typename T>
class Cache
{
public:
    class Proxy : public T
    {
        friend class Cache;

    private:
        std::list<Proxy> & m_refList;
        typename std::list<Proxy>::iterator m_clsPosition;

        Proxy(std::list<Proxy> & refList) : m_refList(refList) {}
    };

private:
    std::list<Proxy> m_clsList;
    typename std::list<Proxy>::iterator m_clsCurrent;

    static void Release(Proxy * ptrProxy)
    {
        ptrProxy->m_refList.splice(ptrProxy->m_refList.m_clsCurrent,
                                   ptrProxy->m_refList,
                                   ptrProxy->m_clsPosition);
        if ( ptrProxy->m_refList.m_clsCurrent == ptrProxy->m_refList.end() )
            --(ptrProxy->m_refList.m_clsCurrent);
    }

public:
    Cache() {m_clsCurrent = m_clsList.end();}
    ~Cache()
    {
        if ( m_clsList.size() && m_clsCurrent != m_clsList.begin() )
        {
            // ERROR - Cache not empty
        }
    }

    typedef boost::shared_ptr<Proxy> Ptr;

    static Ptr Get()
    {
        static boost::thread_specific_ptr<Cache> clsCache;
        if ( clsCache.get() == NULL )
            clsCache.reset(new Cache());

        Proxy * ptrProxy;
        if ( clsCache->m_clsCurrent == clsCache->m_clsList.end() )
        {
            clsCache->m_clsList.push_front(Proxy(clsCache->m_clsList));
            ptrProxy = &(clsCache->m_clsList.front());
            ptrProxy->m_clsPosition = clsCache->m_clsList.begin();
        }
        else
        {
            ptrProxy = &(*(clsCache->m_clsCurrent));
            ptrProxy->m_clsPosition = clsCache->m_clsCurrent++;
        }
        return Ptr(ptrProxy, Release);
    }
};

编译错误在typename std::list<Proxy>::iterator m_clsPosition行:

No type named 'iterator' in 'std::__1::list<ASW::Cache<std::__1::basic_string<char>>::Proxy, std::__1::allocator<ASW::Cache<std::__1::basic_string<char>>::Proxy>>'

(Cache 的模板参数是 std::basic_string<char> )

我了解发生了什么 - 我正在引用 Proxy在它被完全定义之前。但是为什么 iterator需要 Proxy 的定义编译?

数据结构的原因有两个:1) 回收对象而不是销毁它们,以及 2) 通过将缓存对象中的迭代器保持在其在列表中的位置来加快回收。如果有人对如何实现这些有更好的想法(前提是无法修复此错误),我很想听听。

最佳答案

您的代码具有未定义的行为,因为您正在使用不完整的类型实例化 std::list(Proxy 尚未在其定义中定义)。 [res.on.functions]/2:

... the effects are undefined ... if an incomplete type is used as a template argument when instantiating a template component, unless specifically allowed for that component.

尝试使用 list 的实现 works with incomplete types ,例如 boost::container::list .

关于c++ - 模板类的内部类不会在 XCode 4.5.2 上编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15102111/

相关文章:

ios - 检查日期是否存在于 objective-c 中的这一年

枚举类中的 C++ 运算符重载

c++ - 作为参数传递的打印函数指针导致在屏幕上打印 '1'

C++ 抽象类模板

c++ - 将数据从一个类传递到另一个类而不传递对象

c++ - 如何在带有模板的函数中使用前向类声明?

ios - SwiftHTTP 已安装但无法导入

javascript - 是否可以在 Macgap 中调试 UIWebView?

c++ - 没有可变参数模板的通用分配器类?

php - CodeIgniter 创建布局/模板库