0
#include <iostream>
#include <vector>
#include <string>
#include <any>
#include <map>
#include <functional>
#include <exception>
using namespace std;
using MapAny = std::map<string, any>;

int square(int x) {
    return x*x;
}

vector<int> parse(map<string, vector<MapAny>> mapping)
{
    vector<MapAny> func_square = mapping["square"];
    vector<int> res;
    for (const auto &mapany : func_square) {
        try {
            int x = any_cast<int>(mapany.at("x"));    
            res.push_back(square(x));
        }
        catch (exception e) {
            vector<int> xs = any_cast<vector<int>>(mapany.at("x"));
            for (int x : xs) res.push_back(square(x));
        }        
    }

    return res;
}

int main()
{
    map<string, vector<MapAny>> function_map_value, function_map_array;
    function_map_value = {
        {"square", { {{"x", 5}}, {{"x", 10}} }}
    };
    
    vector<MapAny> vec;    
    vec.push_back({{"x", vector<int>({5, 10}) }});
    function_map_array = {
        {"square", vec}
    };

    vector<int> res1 = parse(function_map_value);
    vector<int> res2 = parse(function_map_array);
    for (int i=0; i<res1.size(); i++) cout << res1[i] << " "; cout << "\n";
    for (int i=0; i<res2.size(); i++) cout << res2[i] << " "; cout << "\n";
    return 0;
}

I'm trying to make a function parser that can accept any type, such as both scalar and vector values, like in Python dict().

However, I'm not sure how to check if a std::any object has type std::vector. In the code above, if any_cast fails, it will throw exception and I know it's a std::vector. It's ugly and rely on throwing exception as an expected behavior.

How can I change the above code into something like:

if (is_vector(mapany.at("x")) {
  // deal with vector
}
else {
  // deal with scalar
}
Huy Le
  • 1,439
  • 4
  • 19
  • 2
    That is not possible, `std::any` is only useful if you have a known set of possible types at any given point of use. The use cases for `std::any` are very limited. You cannot write functions working on completely arbitrary types at runtime. – user17732522 Mar 28 '22 at 03:55
  • Is the above possible with std::variant ? – Huy Le Mar 28 '22 at 03:56
  • No, the same applies to `std::variant`. However it is much more likely that `std::variant` is a suitable tool for your use case than that `std::any` is. – user17732522 Mar 28 '22 at 03:58
  • 1
    Don't try to translate Python to C++ function-by-function. It is not going to go well. What is your overall goal here? – user17732522 Mar 28 '22 at 03:58
  • Maybe I am misunderstanding your question: Are you asking how to check for `std::vector` specifically (possible) or `std::vector` for any type `T` in general (not possible)? – user17732522 Mar 28 '22 at 04:04
  • @user17732522 I'm trying to check if a std::any object is a std::vector regardless of type `T`. It just need to be a vector – Huy Le Mar 28 '22 at 04:31
  • @HuyLe What would you do with that information if you could get it? You will not be able to cast to it, because std::vector is not a type, but a template. – gerum Mar 28 '22 at 08:00
  • Sorry, I thought checking for vector of any T is easier. I know for sure that for function `square`, it's `vector`, for another function `weight_mean` and `parse_weight_mean` it can be `vector`. – Huy Le Mar 28 '22 at 09:06
  • @user17732522 can you describe the solution to check for `vector` specifically? My previous comment was misleading – Huy Le Mar 28 '22 at 09:07
  • Does this answer your question? [Checking std::any's type without RTTI](https://stackoverflow.com/questions/63524970/checking-stdanys-type-without-rtti) – user17732522 Mar 28 '22 at 11:57

0 Answers0