c++ - boost::optional reference with boost::variant type -
i'm writing code game , part of involves creating history of actions have taken place far in game. history stored in vector of state_pair_t
's pairs of actions (action_t
's) , pointers result gamestate after action has been made. have function looks through history starting @ recent point in time , iterates backwards until action of type found returns reference that. decided might design move use boost optional return no_action
if no action found , use boost::optional
deal these functions should return value might not have value return. when i've tried implement run error don't understand:
typedef boost::variant< a, b, b > action_t; typedef boost::optional< action_t& > opt_action_ref_t; const opt_action_ref_t no_action = opt_action_ref_t(); /*! state pair combination of particular action , resulting game state */ typedef std::pair< const action_t, game_state_ptr > state_pair_t; opt_action_ref_t get_last_non_a_action() const{ std::vector< state_pair_t >::const_reverse_iterator rcit; for(rcit = m_states.rbegin(); m_states.rend() != rcit ; ++rcit){ if(!(is_action_type< >(rcit->first))){ return rcit->first; \\error @ compile time } } return no_action; }
now gives compile error:
error error c2664: 'boost::optional<t>::optional(boost::none_t)' : cannot convert parameter 1 'const action_t' 'boost::none_t'
now if change to:
if(!(is_action_type< >(rcit->first))){ return boost::optional<action_t>(rcit->first); }
i error:
error c2664: 'boost::optional<t>::optional(boost::none_t)' : cannot convert parameter 1 'boost::optional<t>' 'boost::none_t'
i'm not sure either of these errors trying tell me here. i'm trying here not good idea boost::optional
? possible?
optional references in idea; they're mentioned such in recent paper n3527, intended standardisation library component in c++14. they're supported in boost.optional.
the problem code you're trying bind non-const optional reference const lvalue; if change boost::optional< action_t& >
boost::optional< const action_t& >
should compile fine.
Comments
Post a Comment