在下面的示例中,最后一行失败,因为
JSON
的转换运算符必须在两个可能性之间进行选择:
std::vector<int>::operator=(std::vector<int>&&)
std::vector<int>::operator=(std::initializer_list<int>)
JSON::operator T()
以忽略 initializer_list
重载?struct JSON
{
using StorageType = std::variant<
int,
float,
std::string,
std::vector<int>,
std::vector<float>
>;
StorageType storage;
template<class T>
operator T() const {
return std::get<T>(storage);
}
};
int main(int argc, char* argv[])
{
const JSON json;
std::vector<int> numbers;
numbers = json; // more than one operator '=' matches these operands
return 0;
}