Hi ToohrVyk,
I posted a reply, but then realized I needed to look at a couple of things more carefully. Here's my second try :)
Your programming expertise exceeds mine, so I'm mostly posting this for my own edification. However, it seems boost::variant could be used here, only using an empty 'no hit' tag rather than a 'meaningless' int.
Here's some code I wrote up to test the idea:
#include <boost/variant.hpp>#include <iostream>using std::cout;using std::endl;struct NoHit {};struct Hit { float distance; Hit(float distance) : distance(distance) {}};class Scene{public: typedef boost::variant<NoHit,Hit> hit_t; const hit_t& Cast(bool hit) { // 'hit' argument is for testing purposes if (hit) { m_hit = Hit(1.f); } else { m_hit = NoHit(); } return m_hit; }private: hit_t m_hit;};struct ProcessHit : boost::static_visitor<> { void operator()(const NoHit& hit) const { cout << "No hit" << endl; } void operator()(const Hit& hit) const { cout << "Hit: distance is " << hit.distance << endl; }};int main(){ Scene scene; boost::apply_visitor(ProcessHit(), scene.Cast(true)); boost::apply_visitor(ProcessHit(), scene.Cast(false));}
This doesn't quite meet your requirements, since Hit must be assignable in order for it to work. It's not immediately clear to me though how the same variant object can be reused if Hit is not made to be assignable.
Again, I'm more or less out of my league answering any question that you would post, but I'm mostly just replying out of interest. In any case, I'll be curious to see what others have to say.