gpt4 book ai didi

c++ - 为什么 is_copy_constructible 在 MSVC12 中为 unique_ptr 返回 true

转载 作者:可可西里 更新时间:2023-11-01 15:07:21 28 4
gpt4 key购买 nike

我原以为这个静态断言会触发:

#include <type_traits>
#include <memory>

int main() {
static_assert(std::is_copy_constructible<std::unique_ptr<int>>::value, "UPtr has copy constructor?");
}

但事实并非如此。

使用 MSVC12 编译:

Microsoft (R) C/C++ Optimizing Compiler Version 18.00.31101 for x64

最佳答案

static_assert 应该触发,std::unique_ptr有一个隐式删除的复制构造函数,所以这是一个错误。这看起来与此错误报告有关 std::is_copy_constructible is broken :

(1) std::is_copy_constructible returns true for types with deleted copy constructors.

(2) std::is_copy_constructible returns true for types that compose types that are not copy constructible.

响应是:

Thanks for reporting this bug. We've fixed it, and the fix will be available in the next major version of Visual Studio after 2013.

另请参阅此错误报告:std::is_copy_constructible doesn't work correctly .

请注意断言在 webcompiler 上触发它使用的是最新版本的 Visual Studio。最后一次更新是在 2015 年 12 月 3 日。该断言还会在 clang( see it live ) 和 gcc 上触发。

我发现了一个错误报告:A strange behavior of std::is_copy_constructible它与您的代码非常相似:

static_assert(std::is_copy_constructible<std::unique_ptr<int>>::value, "");

那里的回应是:

Thanks for reporting this bug. We've already fixed it, and the fix is available in VS 2015 Preview.

不清楚,这是在哪个版本的 Visual Studio 中修复的。一个回复说是 2013 年末版本,而后一个回复说是 2015 预览版。

关于c++ - 为什么 is_copy_constructible 在 MSVC12 中为 unique_ptr 返回 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34135409/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com