0

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;
}
cigien
  • 57,834
  • 11
  • 73
  • 112
greatusername
  • 129
  • 1
  • 10

1 Answers1

3

std::map by default requires that the key type be comparable with <. std::any does not define any operator< 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::string array as key or value type, then use std::array<std::string, N> or std::vector<std::string> instead of a plain std::string[N].

The std::map will work with these types out-of-the-box.

std::any has 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.

user17732522
  • 53,019
  • 2
  • 56
  • 105