<分区>
Boost Variant 文档说明了接受任意类型的构造函数的以下内容:
template<typename T> variant(T & operand);
- 要求:T 必须明确可转换为一种有界类型(即 T1、T2 等)。
接受const T&
和T&&
的构造函数也是如此。所以我预计以下代码不会编译:
boost::variant<std::string, bool> v = "text";
但是代码编译后,v
变成了 bool,这是我绝对不想要的。当然,解决方案是将字符串文字包装在 std::string
构造函数中。我的问题是:
- 为什么这段代码可以编译?
- 它如何选择类型(因为
const char*
可以转换为std::string
和bool
)?