如果我使用 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++ - nullptr 作为 boost::any 映射的标志值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18059924/