c ++ initializer_list和shared_ptr行为

 彭木对_690 发布于 2023-02-13 17:58

我正在测试vs2013 c ++ initializer_list.

可以编译下面的代码.但是当我运行exe时崩溃了.

#include 
#include 

class Base {};

class Derived : public Base {};

void DoSomething(std::initializer_list > list)
{
}

int main()
{
  auto ip = std::make_shared();

  std::cout << "use_count=" << ip.use_count() << std::endl;

  DoSomething({ip, std::make_shared()}); // ng
  // DoSomething({ip, std::make_shared()}); // ok
  // DoSomething({std::make_shared(), ip}); // ok

  std::cout << "use_count=" << ip.use_count() << std::endl;
}

编译.

C:\...>cl.exe /EHsc test.cpp
Microsoft(R) C/C++ Optimizing Compiler Version 18.00.21005.1 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

test.cpp
Microsoft (R) Incremental Linker Version 12.00.21005.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test.exe
test.obj

C:\...>

我期待这样的输出.g ++ 4.8.2就是这样的.

c:\...>test.exe
use_count=1
use_count=1

但是,看起来像这样.

c:\...>test.exe
use_count=1
use_count=0 // or some random value like 3719232 and displayed crash dialog. 

并且,修改上面代码的一行,这很好用.

DoSomething({std::make_shared(), ip});

这是vs2013 initializer_list的错误或正常行为吗?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有