boost::variant 的树状容器——有什么缺点吗?

tree-like containers of boost::variant -- are there any drawbacks?

显然可以 有序 映射和 boost::variant 集合,像这样:

typedef boost::variant<std::string, int, bool> key_type;
std::map<key_type, size_t> m;

m.insert(std::make_pair(std::string("a"), 3));
m.insert(std::make_pair(1, 7));
auto x = m.find(1);
std::cout << x->first << " " << x->second << "\n";
x = m.find(std::string("a"));
std::cout << x->first << " " << x->second << "\n";

输出:

1 7
a 3

不过,我觉得有点可疑;我查看了源代码,看看它是如何工作的,但并没有从中得到太多……不知何故,不同的类型必须与 operator< 进行比较……这需要为任何 2类型;除了将不同类型与 < 进行比较对我来说本身没有任何意义。因此,我想知道在性能方面使用地图或 boost::variant 集是否有问题。有陷阱吗?或者可以有地图或 boost::variant 套吗?

我最终在文档中找到了它:http://www.boost.org/doc/libs/1_63_0/doc/html/variant/reference.html

Every type specified as a template argument to variant must at minimum fulfill the above requirements. In addition, certain features of variant are available only if its bounded types meet the requirements of these following additional concepts:

...

LessThanComparable: variant is itself LessThanComparable if and only if every one of its bounded types meets the requirements of the concept.

因为 stringintbool 都是 LessThanComparable 那么在这种情况下代码是安全和正确的。