首页 > 解决方案 > 是什么阻止了这段代码在 Visual C++ 6 中编译?

问题描述

我正在查看这个 GitHub 存储库,以替换 Visual C++ 6 中的 std::shared_ptr 以修改一些旧代码,但我发现这个库不兼容,因为编译器抛出了解析器错误。

我知道支持嵌套模板类和朋友类。但这是一个template<class U>与模板不同的地方class shared_ptr,也是朋友类引用自己的地方。

这个自引用模板朋友类的目的是什么?它是如何使用的?

shared_ptr无需编译引用的库即可演示该问题:

template<class T>
class shared_ptr
{
    template<class U>
    friend class shared_ptr;
};

int main() {
   return 0;
}

编译器抛出以下错误:

main.cpp(5) : error C2059: syntax error : '<end Parse>'
        main.cpp(6) : see reference to class template instantiation 'shared_ptr<T>' being compiled
main.cpp(6) : error C2143: syntax error : missing ';' before '}'
        main.cpp(6) : see reference to class template instantiation 'shared_ptr<T>' being compiled
main.cpp(6) : error C2238: unexpected token(s) preceding ';'
        main.cpp(6) : see reference to class template instantiation 'shared_ptr<T>' being compiled

我不需要这个库,因为我已经能够通过使用 Boost 1.34.1 解决这个问题。但我想了解这个限制以及它的用途。

标签: c++c++98visual-studio-6

解决方案


推荐阅读