我对使用 std::variant 不是很熟练,也不能完全理解所有构造函数的情况。
using map_type = std::multimap<std::string, std::string>;
using union_type = std::variant<std::string, map_type>;
void foo(union_type)
{
...
}
void foo2(map_type)
{
...
}
int main()
{
foo({ {"key", "val"} }); // gives error "no instance of constructor"
foo2({ {"key", "val"} }); // works fine
foo2(map_type{ {"key", "val"} }); // works fine
}
正如我可以推断出的,这里调用了构造函数,但不明白什么条件不满足。有人可以解释一下吗?
PS是否有一些文章或来源可以阅读更多信息和使用变体的示例?