Question

If I set up a generic container map using boost::any, and use the new nullptr from C++11 as an initialization value akin to an isset() type operation, are there any potential pitfalls?

For example:

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;
    }
}

Before C++11, I had the boost::any wrapped in a struct with a bool isset, but this seems to be working. Any pitfalls?

Was it helpful?

Solution

I can't see any pitfalls as such (beyond being careful to specifically set it to nullptr_t and not any other pointer type); but why not just leave it empty, and check it with its empty() member function?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top