nullsquared wrote:Sounds like you're using boost::any wrong. It's meant for passing around any value type (like void*) that is typed at runtime. For example, if you have a GUI update function, you might want a "miscellaneous" parameter that can hold mouse data, button data, etc. Depending on the situation, you're assuming to know what the type is, and you're allowed to cast it. This is such that you can use containers of any's, etc. For example, boost::any is very useful with Lua...
Yes, after checking, I want it to work in a way that is "wrong". According to the docs: "Discriminated types that contain values of different types but do not attempt conversion between them." For anyone listening in, if you pass IBaseObject * to a boost::any object, the only boost::any_cast that will work is back to IBaseObject *. Granted I never tried to cast back to anything!
But, I disagree that it is "typed at runtime." The any object is typed at compile time. Ya, the any_cast is "run-time" in the sense that it returns a good pointer or NULL using RTTI clone code, but the assignment of the any object is a templated placeholder type that occurs at compile time based on the compile time argument's type to the any constructor.
nullsquared wrote:If you're using a factory or something, then there's no point in putting the interface inside an any. Just use the interface itself.
I think I should fill in the gaps... From a library standpoint, I have no idea what the user's interface is, and I will not extend any library object from a user's class... that would be a bad design move. The only other alternative is to force the user to extend some base library object... which would defeat the point of the any object.
The reason I had problems with this at the library level is because I needed to make decisions in my library based on RTTI information about the user's objects (without knowing anything about the object or forcing the user to inherit my objects). The any object's type_info is taken from a template parameter, which directly ties it to the compile-time type. Since the user simply stored IBase *, all of the type_info provided by any directly exposed IBase * and nothing useful. All of the placeholder's are templated to IBase *, etc. But at compile time, my library doesn't have any useful headers to include to do this. Point is: if you try to use any to do anything outside of storing data for someone who knows the actual type at compile time, even if all you are doing is comparing types of user data at run-time, any objects are not sufficient because they are wired at compile-time.