为什么对于MSVC12中的unique_ptr,is_copy_constructible返回true
Why does is_copy_constructible return true for unique_ptr in MSVC12
我本以为会触发这个静态断言:
#include <type_traits>
#include <memory>
int main() {
static_assert(std::is_copy_constructible<std::unique_ptr<int>>::value, "UPtr has copy constructor?");
}
但事实并非如此。
使用MSVC12:编译
用于x64 的Microsoft(R)C/C++优化编译器18.00.31101版
static_assert
应该激发,std::unique_ptr有一个隐式删除的复制构造函数,所以这是一个错误。这看起来与此错误报告有关std::is_copy_constructible已损坏:
(1) std::is_copy_constructible对于已删除的类型返回true复制构造函数。
(2) std::is_copy_constructible对于组成的类型返回true不可复制构造的类型。
结果是:
感谢您报告此错误。我们已经修复了它,该修复程序将在2013年之后的Visual Studio下一个主要版本中提供。
另外,请参阅此错误报告:std::is_copy_constructible无法正常工作。
请注意,断言在使用最新版本Visual Studio的Web编译器上激发。上次更新是在Dec 3, 2015
上。断言还对clang(see it live)和gcc启动。
我发现了一个错误报告:std::is_copy_constructible的一个奇怪行为,它的代码与您的代码非常相似:
static_assert(std::is_copy_constructible<std::unique_ptr<int>>::value, "");
那里的反应是:
感谢您报告此错误。我们已经修复了,修复是在VS 2015预览版中提供。
目前还不清楚这是在Visual Studio的哪个版本中修复的。一个回复说是2013年末的版本,而后面的回复说是2015预览版。
以下是四种使类不可复制的方法:
#include <stdio.h>
#include <type_traits>
class A {
public:
A(const A&) = delete;
void operator=(const A&) = delete;
};
class B {
private:
B(const B&) = delete;
void operator=(const B&) = delete;
};
class C {
public:
C(const C&) = delete;
void operator=(const C&) = delete;
void operator=(C) = delete;
};
class D {
private:
D(const D&) = delete;
void operator=(const D&) = delete;
void operator=(D) = delete;
};
int main() {
printf("%d %dn", std::is_copy_constructible<A>::value, std::is_copy_assignable<A>::value);
printf("%d %dn", std::is_copy_constructible<B>::value, std::is_copy_assignable<B>::value);
printf("%d %dn", std::is_copy_constructible<C>::value, std::is_copy_assignable<C>::value);
printf("%d %dn", std::is_copy_constructible<D>::value, std::is_copy_assignable<D>::value);
}
在MSVC2013 x64(18.00.40629 for x64
)上,它打印:
1 1 //A
0 1 //B
1 0 //C
0 0 //D
在正确的编译器上,所有八个值都必须为零。
不幸的是,这并不是提供一个很好的方法来解决MSVC2013中的错误,即使对于您自己的类也是如此。因为如果您通过值声明接受参数的赋值运算符,则不能在同一类中声明移动赋值(由于不明确的重载,任何移动赋值都不会编译)。
p.S.修正分配的关键思想取自这个相关答案。
- std::filesystem::copy throws filesystem_error
- Why is UINT32_MAX + 1 = 0?
- C++错误:"error: int aaa::bbb is protected within this context"
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- 如何在 Boost.Asio 中使用 Zero-copy sendmsg/receive
- Is !NaN not a NaN?
- Directx 11 - CompileFromFile() is not compiling
- Centos7 g++ "to_string is not in a member of std"
- "Unable to start debugging. No process is associated with this object." - 在Visual Studio Code中使用GDB
- Where is std::hardware_destructive_interference_size?
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- What is unordered_set in C++
- 你如何理解"std: :forward is just syntactic sugar"?这是真的吗?
- C++ "error: use of overloaded operator '*' is ambiguous"似乎只有一场比赛
- C++ is calculating wrong
- Is it good to use SDL_PIXELFORMAT_UNKNOWN?
- C++ 一个lambda浅拷贝const Type&如果它被赋予一个命名捕获,如[copy=val](){}?
- 这在C++ "It does not own the underlying data, and so is cheap to copy or assign"中意味着什么
- 处理"Thrown exception type is not nothrow copy constructible"警告