Я вставляю некоторые данные, обернутые boost :: any, в карту в методе класса getValue. Все работает нормально, данные на карте, если нужно, и т. Д. В getValue. Когда я оставляю getValue и пытаюсь использовать эти данные, их больше нет на карте.
Это поставило меня в тупик, я, наверное, забыл использовать ссылку в ключевом месте, но не могу ее найти.
Соответствующий код выглядит следующим образом:
test.c
// Мы не хотим задокументировать таймер, пользователь может сделать это, если захочет.
timeval tmp;
tmp.tv_sec = 0;
tmp.tv_usec = 0;
gettimeofday(&tmp, NULL);
getValue<timeval>(timerName) = tmp;
std::cout << tmp.tv_usec << " : " << getSingleton().globalValues.count(key) << std::endl; //Count returns 0 here, for a given key X_X
test.h
/* Grab the value of type T found while parsing. Should use checkValue first.*/
template<typename T>
static T& getValue(const char* identifier) {
//Used to ensure we have a valid value
T tmp;
//Used to index into the globalValues map
std::string key = std::string(identifier);
std::map<std::string, boost::any>& gmap = getSingleton().globalValues;
if(checkValue(identifier)) //If we have the option, set it's value
tmp = getSingleton().vmap[identifier].as<T>(); //vmap is correct, it specifies default values passed in via command line.
//We may have whatever is on the commandline, but what if
//The programmer has made modifications?
if(!gmap.count(key)) //The programmer hasn't done anything, lets register it then
gmap[key] = boost::any(tmp);
std::cout << "gmap " << key << std::endl;
std::cout << getSingleton().globalValues.count(key) << std::endl; //count returns 1 here, for a given key.
return boost::any_cast<T&>(gmap[key]);
}
...
test.h
//Map of global values, stored here instead of in OptionsHierarchy
//For ease of implementation
std::map<std::string, boost::any> globalValues;