Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with the assignment operator #18

Open
knachte opened this issue Feb 6, 2023 · 1 comment
Open

Problem with the assignment operator #18

knachte opened this issue Feb 6, 2023 · 1 comment

Comments

@knachte
Copy link

knachte commented Feb 6, 2023

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
Copy link
Author

knachte commented Feb 6, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant