nullptr作为boost::any映射的标志值
nullptr as flag value for boost::any map
如果我使用boost::any
设置一个通用容器映射,并使用C++11中的新nullptr
作为类似于isset()
类型操作的初始化值,是否存在任何潜在的陷阱?
例如:
std::map<std::string, boost::any> map;
map["A"] = nullptr;
map["B"] = nullptr;
map["C"] = nullptr;
map["D"] = nullptr;
map["A"] = 1;
map["C"] = 3;
// assume error checking, other types, etc.
for(auto k : map) {
if (k.second.type() != typeid(nullptr)) {
std::cout << k.first << " : " << boost::any_cast<int>(k.second) << std::endl;
}
}
在C++11之前,我将boost::any
封装在一个带有bool isset
的结构中,但这似乎是可行的。有陷阱吗?
我看不到任何陷阱(除了要小心将其专门设置为nullptr_t
,而不是任何其他指针类型);但为什么不让它为空,并用它的empty()
成员函数检查它呢?
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 将函数类成员映射到类本身内部
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 使用std::函数映射对象方法
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- C++映射分割错误(核心转储)
- 内联映射初始化的动态atexit析构函数崩溃
- 使用"std::unordereded_map"映射到"std::list"对象
- 如何从多映射中删除特定的重复项
- 在未初始化映射的情况下,将值插入到映射的映射中
- QT通过C++添加映射QML项目
- 在c++中访问int到类对象的映射时出错
- 在C++中搜索嵌套多映射值
- 错误处理.将系统错误代码映射到泛型
- C++匿名结构作为std::映射值
- 如何从存储在std::映射中的std::集中删除元素
- nullptr作为boost::any映射的标志值
- std::映射<>或std::vector<>在处理大型标志集时