nlohmann / fifo_map

a FIFO-ordered associative container for C++
MIT License
179 stars 77 forks source link

Problem with the assignment operator #18

Open knachte opened 1 year ago

knachte commented 1 year ago

i might be doing something wrong, but i'm having some problems with the = operator. When i build a fifo_map in a function and then return it, it seems to be ok, i can iterate over it. However, as soon as i try to use a find on it, i get a segmentation fault.

In the example below i test with both a map and a fifo_map, the map seems to be working as expected, fifo_map doesn't.

When i do the assign with the copy constructor it does seem to be working.

#include <iostream>
#include <map>
#include "fifo_map.hpp"
using nlohmann::fifo_map;

fifo_map<std::string, std:: string> generate_fifo() {
    fifo_map<std::string, std:: string> test;
    test["one"] = "first";
    test["two"] = "second";
    test["three"] = "third";
    return test;
}

std::map<std::string, std:: string> generate_map() {
    std::map<std::string, std:: string> test;
    test["one"] = "first";
    test["two"] = "second";
    test["three"] = "third";
    return test;
}

int main(int argc, char* argv[]) {
    std::map<std::string, std:: string> map_test;
    fifo_map<std::string, std:: string> fifo_test;
    map_test = generate_map();
    fifo_test = generate_fifo();

    for (const auto &item : map_test) {
        std::cout << item.first << " -> " << item.second << std::endl;
    }

    auto it = map_test.find("two");
    if (it != map_test.end()) {
        std::cout << "found" << std::endl;
    } else {
        std::cout << "not found" << std::endl;
    }

    for (const auto &item : fifo_test) {
        std::cout << item.first << " -> " << item.second << std::endl;
    }

    auto it2 = fifo_test.find("two");
    if (it2 != fifo_test.end()) {
        std::cout << "found" << std::endl;
    } else {
        std::cout << "not found" << std::endl;
    }
}
knachte commented 1 year ago

An update to this, it only seems to be happening when compiling as c++ 20 with gcc 9.3. When switching back to c++17 it works as expected. When compiling as c++ 20 for windows with visual studio it also works without a problem.