A vector filled with templates or whatnot.

Started by
3 comments, last by Agony 17 years ago
I'm creating a configuration file loader and I want to create a vector of a data class that happens to be a template of the data that was loaded from the configuration file (Ex, int, string, doubles, user defined types, etc.) I also wanted the data class to hold a pointer to the variable that would keep up with the changes as the game went on, so when I went to write and load, it would be almost seamless. Is there anyway to keep a vector of a template class?
Advertisement
Sure.
std::vector<type<T> >   vectorName;


As for a vector that holds variant types? That's messy and ill-advised. Boost though has variant classes if you're deadset upon it.
struct Holder{  virtual int asInt() = 0;  virtual std::string asString() = 0;};template <typename T>struct TypedHolder : Holder{  virtual int asInt()  {    return m_value;  }  virtual std::string asString()   {    // bad way to convert, not even sure if it works    return std::string (itoa(m_value,10));  }private:  T m_value;}template <>struct TypedHolder<std::string>{  virtual int asInt()  {    throw // bad cast  }  virtual std::string asString()  {    return m_value;  }private:  std::string m_value;}std::list< Holder * > values;


But obviously, the better choice here would be boost classes, which already provide all of that. Any or Variant classes will give you all this in very robust form.

In addition, you have the serialization and program-options which can take care of all persistence and configuration.

[Edited by - Antheus on March 27, 2007 8:58:20 AM]
Yeah, I thought I was going to have to do something like that. I've never heard of the Boost libs though. Seems pretty cool, thanks for the infos. Would you recommend using this library for the others things inside of it too? Do you use the lib?
The Boost library is awesome, containing all sorts of useful stuff. Some of it is also likely to become part of the C++ Standard Library when the next standard is finally released (2009-ish, I believe?).
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke

This topic is closed to new replies.

Advertisement