Search code examples
c++c++17stdany

unordered_map with std::any values cannot any_cast string


I'm new to C++ and I was studying about std::unordered_map and std::any. I've created a sample demo bellow which generates some dummy data and then they get inserted into the map.

After that (in the commented out code) I print out the values using any_cast with success.

However at the bottom as you may see I try to retrieve a specific key with 0 success. The error I'm getting is Bad any_cast and the casting is exactly the same as the code I used for printing (which is currently commented out).

I'm sorry if this is a silly mistake but I'm quite new. Thank you in advance.

#include <iostream>
#include <string>
#include <any>
#include <map>
#include <unordered_map>
#include <time.h>
#include <Windows.h>

std::unordered_map<std::string, std::any> map = {};

int main() {

    unsigned long started = clock();
    const std::string prefix = "key";

    for (int i = 0; i < 1000000; i++) {

        const std::string key = "key" + std::to_string(i);

        map.insert_or_assign(key, i);

    }

    std::cout << "Data inserted after: " << (clock() - started) << "ms" << std::endl;

    system("pause");

    started = clock();

    /*
    for (const auto& item : map) {


        try {

            //std::cout << item.second.type().name() << std::endl;
            if (item.second.type() == typeid(int)) {
                std::cout << std::any_cast<const int>(item.second) << std::endl;
            }
            else if (item.second.type() == typeid(float)) {
                std::cout << std::any_cast<const float>(item.second) << std::endl;
            }
            else if (item.second.type() == typeid(double)) {
                std::cout << std::any_cast<const double>(item.second) << std::endl;
            }
            else if (item.second.type() == typeid(long)) {
                std::cout << std::any_cast<const long>(item.second) << std::endl;
            }
            else if (item.second.type() == typeid(char const *)) {
                std::cout << std::any_cast<char const *>(item.second) << std::endl;
            }
            else if (item.second.type() == typeid(std::string)) {
                std::cout << std::any_cast<const std::string>(item.second) << std::endl;
            }
            else {
                std::cout << item.first << " has an unhandled value type of " << item.second.type().name() << std::endl;
            }

        }
        catch (const std::bad_any_cast& err) {
            std::cerr << err.what() << std::endl;
        }



    }

    std::cout << "Map iterated after: " << (clock() - started) << "ms" << std::endl;

    */

    try {

        auto value = std::any_cast<char const *>(map["key8745"]);

        std::cout << "Key " << value << " retrieved after: " << (clock() - started) << "ms" << std::endl;

    }
    catch (const std::bad_any_cast &err) {
        std::cerr << err.what() << std::endl;
    }

    system("pause");

    return 0;

}

Solution

  • All the values you insert are ints, but you're trying to cast the single value at the bottom to char const*; it's not a pointer of any kind, and it looks like any_cast is sufficiently type safe to reject that attempt to cast (a good thing, since dereferencing a pointer to memory address 8745 is likely to segfault). You're type checking in the earlier code and presumably selecting the correct cast there.