I was looking for a way to have a string[] as a map value, and I found this Stack Overflow question. I attempted to use the std::any type to solve my problem, and I got the error
binary '<': 'const _Ty' does not define this operator or a conversion to a type acceptable to the predefined operator
Here is my code:
#include <iostream>
#include <map>
#include <any>
using namespace std;
map<any,any> dat;
int main()
{
any s[] = {"a","b"};
dat["text"] = s;
return 0;
}
std::mapby default requires that the key type be comparable with<.std::anydoes not define anyoperator<and can therefore not be used as key in the map by default.If you really want to use it in the map as a key, you need to implement your own comparator, see this question.
However, the comparator needs to define a weak strict order. It is probably not going to be easy to define that for
std::any.It is unlikely that
map<any,any>is the correct approach to whatever problem you are trying to solve.If you want a
std::stringarray as key or value type, then usestd::array<std::string, N>orstd::vector<std::string>instead of a plainstd::string[N].The
std::mapwill work with these types out-of-the-box.std::anyhas only very few use cases. If you don't have a very specific reason to use it, you probably shouldn't try to use it.