V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
hailongs
V2EX  ›  C

boost::unordered_map 这东西有办法显示析构吗

  •  
  •   hailongs · 2017-03-22 13:09:57 +08:00 · 1903 次点击
    这是一个创建于 2584 天前的主题,其中的信息可能已经有所发展或是发生改变。

    貌似不能直接~unordered_map<int,int>()这样做

    5 条回复    2017-03-22 18:55:42 +08:00
    hyq
        1
    hyq  
       2017-03-22 13:55:10 +08:00
    boost::unorderd_map<int, int> *mymap = (boost::unorderd_map<int, int> *)malloc(sizeof(boost::unorderd_map<int, int>));
    mymap->boost::unorderd_map<int, int>(); // 手动调用构造函数
    // do something
    mymap->~boost::unordered_map<int, int>(); // 手动析构
    free(mymap);
    hyq
        2
    hyq  
       2017-03-22 13:55:52 +08:00
    刚才写错了,函数名不需要命名空间

    boost::unorderd_map<int, int> *mymap = (boost::unorderd_map<int, int> *)malloc(sizeof(boost::unorderd_map<int, int>));
    mymap->unorderd_map<int, int>(); // 手动调用构造函数
    // do something
    mymap->~unordered_map<int, int>(); // 手动析构
    free(mymap);
    alqaz
        3
    alqaz  
       2017-03-22 15:27:32 +08:00   ❤️ 1
    试下 mymap->~unordered_map();
    还有,我想说的是,你对 map 调用 malloc()然后强转,然后对 map do something,后面居然还要手动调用析构函数,小伙子,你思想很危险啊。
    codehz
        4
    codehz  
       2017-03-22 16:32:19 +08:00   ❤️ 1
    @hyq 用 placement new 不是更好么。。。手动调用构造函数要写一堆重复的东西
    hyq
        5
    hyq  
       2017-03-22 18:55:42 +08:00   ❤️ 1
    @codehz #4 这个可以有
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1681 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:45 · PVG 00:45 · LAX 09:45 · JFK 12:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.