我std::map<const char*, boost::any>
用来存储我的图书馆的设置。每个设置只使用一个基础值类型,我想在配置调用set()
或类似的过程中强制执行这一点。使用正确类型的默认值初始化设置。
这是一些伪代码,希望能显示我想要实现的目标:
using namespace std;
using namespace boost;
void set(map<const char *, any> &settings, const char *key, any &value)
{
if (type_of(value) != type_of(settings[key]) throw wrong_type_exception();
settings[key] = value;
}
是否可以在运行时捕获这样的类型错误?如果可能的话,我不希望在我的 API 中有模板函数。
我已经使用过,但如果这是唯一可行的解决方案,我boost::any
可能会考虑 boost::variant 。which()