hailongs
V2EX  ›  C

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

  •  
  •   hailongs · Mar 22, 2017 · 3147 views
    This topic created in 3519 days ago, the information mentioned may be changed or developed.

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

    5 replies  •  2017-03-22 18:55:42 +08:00
    hyq
    hyq
    Mar 22, 2017
    1
    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
    hyq
    Mar 22, 2017
    2
    刚才写错了,函数名不需要命名空间

    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
    alqaz
    Mar 22, 2017❤️1
    3
    试下 mymap->~unordered_map();
    还有,我想说的是,你对 map 调用 malloc()然后强转,然后对 map do something,后面居然还要手动调用析构函数,小伙子,你思想很危险啊。
    codehz
    codehz
    Mar 22, 2017❤️1
    4
    @hyq 用 placement new 不是更好么。。。手动调用构造函数要写一堆重复的东西
    hyq
    hyq
    Mar 22, 2017❤️1
    5
    @codehz #4 这个可以有
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2580 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 11:09 · PVG 19:09 · LAX 03:09 · JFK 06:09
    ♥ Do have faith in what you're doing.