You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visual Studio 2019中使用std::make_shared触发Error C2440问题求助

VS2019编译std::make_shared<double[]>报错C2440排查

代码示例

#include <memory>

int main()
{
    std::shared_ptr<double[]> sp7 = std::make_shared<double[]>(256, 2.0);
}

上述代码参考C++标准文档实现,但在Visual Studio 2019中编译时出现以下错误:

Error C2440 '=': cannot convert from '_Ux (*const )' to 'double *'

完整构建输出如下:

Build started...
1>------ Build started: Project: Mem, Configuration: Debug Win32 ------
1>Mem.cpp
1>C:\[MSVCdir]\include\xmemory(1893,9): warning C4200: nonstandard extension used: zero-sized array in struct/union
1>C:\[MSVCdir]\include\xmemory(1893,9): message : This member will be ignored by a defaulted constructor or copy/move assignment operator
1>C:\[MSVCdir]\include\memory(2027): message : see reference to class template instantiation 'std::_Wrap<_Ty>' being compiled
1>        with
1>        [
1>            _Ty=double []
1>        ]
1>C:\[MSVCdir]\include\memory(2725): message : see reference to class template instantiation 'std::_Ref_count_obj2<_Ty>' being compiled
1>        with
1>        [
1>            _Ty=double []
1>        ]
1>C:\[localdir]\Test\Mem\Mem.cpp(5): message : see reference to function template instantiation 'std::shared_ptr<double []> std::make_shared<double[],int,double>(int &&,double &&)' being compiled
1>C:\[MSVCdir]\include\memory(1770,1): error C2440: '=': cannot convert from '_Ux (*const )' to 'double *'
1>        with
1>        [
1>            _Ux=double []
1>        ]
1>C:\[MSVCdir]\include\memory(1770,22): message : Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>C:\[MSVCdir]\include\memory(2725): message : see reference to function template instantiation 'void std::shared_ptr<double []>::_Set_ptr_rep_and_enable_shared<_Ty>(_Ux (*const ),std::_Ref_count_base *const ) noexcept' being compiled
1>        with
1>        [
1>            _Ty=double [],
1>            _Ux=double []
1>        ]
1>C:\[MSVCdir]\include\memory(2728): message : see reference to function template instantiation 'void std::shared_ptr<double []>::_Set_ptr_rep_and_enable_shared<_Ty>(_Ux (*const ),std::_Ref_count_base *const ) noexcept' being compiled
1>        with
1>        [
1>            _Ty=double [],
1>            _Ux=double []
1>        ]
1>C:\[localdir]\Test\Mem\Mem.cpp(5): message : see reference to function template instantiation 'std::shared_ptr<double []> std::make_shared<double[],int,double>(int &&,double &&)' being compiled
1>Done building project "Mem.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

错误原因

std::make_shared<T[]>(count, value)这种用指定值初始化数组的重载是C++20标准引入的特性,而Visual Studio 2019自带的MSVC编译器(尤其是较早版本)对该特性的实现存在bug,无法正确处理数组类型的指针转换逻辑,从而触发C2440类型转换错误。

解决方法

  • 升级编译器/IDE:将Visual Studio升级到2022版本,其MSVC编译器对C++20标准的支持更完善,可正常编译该代码。
  • 手动初始化数组(VS2019兼容):放弃使用make_shared,手动创建并初始化数组后传入shared_ptr:
#include <memory>
#include <algorithm>

int main()
{
    double* arr = new double[256];
    std::fill(arr, arr + 256, 2.0);
    std::shared_ptr<double[]> sp7(arr);
}
  • 更新VS2019并配置C++标准:安装Visual Studio 2019的最新更新(推荐16.11及以上版本),同时在项目属性中设置C标准为**ISO C20标准(/std:c++20)**:
    1. 右键项目 -> 属性
    2. 导航到C/C++ -> 语言
    3. 在C++标准下拉菜单中选择ISO C++20标准(/std:c++20)

内容的提问来源于stack exchange,提问作者Sam_W

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 12:37:34