【问题标题】:Does gcc 4.3.4 support unique_ptr?gcc 4.3.4 支持 unique_ptr 吗?
【发布时间】:2013-07-25 06:08:58
【问题描述】:

我一直在尝试在本地 CygWin 安装上使用 g++ 编译器来实现 PIMPL,我开始认为这可能是因为我正在运行 g++ 4.3.4,因为它支持 C++11不够完美。

使用非常基线代码(来自 MSDN here):

my_class.h:
    #include <memory>
    class my_class {
    public:
        my_class();
    private:
        class impl; unique_ptr<impl> pimpl(new impl);
    };

my_class.cpp:
    #include "my_class.h"
    class my_class::impl { int my_int; };
    my_class::my_class(): pimpl( new impl ) {};

我尝试使用g++ -std=c++0x -o my_class.o my_class.cpp 编译并最终得到:

In file included from my_class.cpp:1:
my_class.h:8: error: ISO C++ forbids declaration of 'unique_ptr' with no type
my_class.h:8: error: invalid use of '::'
my_class.h:8: error: expected ';' before '<' token
my_class.cpp: In constructor 'my_class::my_class()':
my_class.cpp:5: error: class 'my_class' does not have any field named 'pimpl'

如果我替换-std=gnu++0x,我也会明白这一点。

事实上,当我尝试编译最简单的文件时,从另一个 SO 答案中提取:

#include <iostream>
#include <memory>
int main() {
    std::unique_ptr<int> up( new int( 30 ) );
}

它抱怨unique_ptr 不在std 命名空间中。

gcc c++11 support page 没有 unique_ptr 的条目,但是从网上看,它已经存在了很长一段时间,至少自 4.4 以来。

所以我的问题是,首先,在哪个版本的gcc 上添加了对unique_ptr 的支持?

其次,我是否只是在代码中犯了一些愚蠢的错误,以错误的方式使用它?

【问题讨论】:

    标签: c++11 g++ unique-ptr


    【解决方案1】:

    在您的第一个示例中,unique_ptr 上缺少 std:: 限定符。第二个例子is correct C++11。

    根据the GCC 4.4 release notes,unique_ptr 在 4.4 之前不在 GCC 的标准 C++ 库中:

    改进了对即将推出的 ISO C++ 标准 C++0x 的实验性支持,包括:

    ...

    • unique_ptr、&lt;algorithm&gt; 添加、异常传播以及对 &lt;string&gt; 和 &lt;limits&gt; 中新字符类型的支持。

    【讨论】:

      猜你喜欢
      • 2016-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-18
      • 2011-02-10
      • 2012-10-06
      相关资源
      最近更新 更多