hailongs
V2EX  ›  C

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

  •  
  •   hailongs · Mar 22, 2017 · 2781 views
    This topic created in 3363 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
        1
    hyq  
       Mar 22, 2017
    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  
       Mar 22, 2017
    刚才写错了,函数名不需要命名空间

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