小编pdo*_*iak的帖子

在VS2015 C++中处理`default`关键字可能存在错误

在测试VS2015 C++编译器时,我偶然发现了default关键字的一个奇怪错误.如果我做:

struct Dummy
{
    Dummy() = default;
    Dummy(const Dummy &) = delete;
};  

int main()
{
    const Dummy& ref = Dummy();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我明白了

错误C2280:'Dummy :: Dummy(const Dummy&)':尝试引用已删除的函数
注释:请参阅'Dummy :: Dummy'的声明

但是,如果我使用一个空构造函数

struct Dummy
{
    Dummy() {}
    Dummy(const Dummy &) = delete;
};

int main()
{
    const Dummy& ref = Dummy();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

代码编译.使用g ++clang运行第一个示例不会产生错误.

为什么在VS2015中使用默认构造函数尝试使用g ++或clang中没有的复制构造函数?

c++ language-lawyer visual-studio-2015

17
推荐指数
1
解决办法
713
查看次数

unique_ptr实现中可能存在的错误

我试图使用带有前向声明的unique_ptr类成员.众多消息来源称例如使用unique_ptr进行前向声明?声明非内联析构函数应该足够了,但在VS2013和GCC 5.3.1中似乎不是这种情况.我没有测试其他编译器.

例:

#include <memory>

class B;

class A { 
public:
    //A();
    ~A();
private:
    std::unique_ptr<B> b;
};

//class B { };

int main() {
    A a;
}
Run Code Online (Sandbox Code Playgroud)

我只能在取消注释ctor声明或类B声明后才能编译此代码.否则在VS2013上我收到错误

error C2338: can't delete an incomplete type
Run Code Online (Sandbox Code Playgroud)

关于GCC错误:

In file included from /usr/local/include/c++/5.3.0/memory:81:0,
                 from main.cpp:1:
/usr/local/include/c++/5.3.0/bits/unique_ptr.h: In instantiation of 'void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = B]':
/usr/local/include/c++/5.3.0/bits/unique_ptr.h:236:17:   required from 'std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = B; _Dp = std::default_delete<B>]'
main.cpp:5:7:   required from here
/usr/local/include/c++/5.3.0/bits/unique_ptr.h:74:22: error: invalid application of 'sizeof' …
Run Code Online (Sandbox Code Playgroud)

c++ unique-ptr c++11

6
推荐指数
1
解决办法
1204
查看次数