【发布时间】:2017-09-02 03:36:27
【问题描述】:
谁能指导我如何解决这个问题。 我有一个 boost::variant。
typedef boost::variant <
int,
std::string,
bool,
double,
vector<int>,
vector<string>,
vector<bool>,
vector<double>
> boostVar;
我正在尝试创建重载 [] 运算符作为类 ABC 的成员函数,类似这样(这只是一个虚拟实现)
class ABC
{
//some map of (key, value) pair that where key is string and value is of type boostVar
boostVar [](const string key)
{
boostVar temp;
//some operation that fills up temp based on value of key
return temp;
}
}
因此,在使用此实现检索特定值时,它会强制用户指定
int key1Val = boost::get<int>(ABC["KEY1"]);
bool key2Val = boost::get<bool>(ABC["KEY2"]);
vector<int> key3Val = boost::get<vector<int>>(ABC["KEY3"]);
我的问题是:
如果我想访问下面的值(i.e. without boost::get<>),我应该如何实现它
int key1Val = ABC["KEY1"];
bool key2Val = ABC["KEY2"];
vector<int> key3Val = ABC["KEY3"];
如果说:KEY1 与 int 不匹配,KEY2 与 bool 不匹配等等,实现应该向用户发出警告。
【问题讨论】:
-
“向用户发出警告”是什么意思?什么形式的警告?您认为在这种情况下会发生什么?
标签: c++ boost operator-overloading implicit-conversion boost-variant