c++ - Extended boost::lexical_cast for other class datatypes -
c++ - Extended boost::lexical_cast for other class datatypes -
is possible extend boost::lexical_cast
handle other datatypes, without modifying classes?
in case, want extend handle things cv::point
, cv::point3
, taking string-separated list of coordinates , loading them.. ability like:
cv::point mypoint = boost::lexical_cast<cv::point>("2,4");
the cv::point
class has stream operators, not compatible istream
, wstream
, fails.
edit
i inquire because i'm working in framework templated function get_parameter
uses boost::lexical_cast
convert string (read configuration file) desired datatype. works great ints & floats, right have phone call multiple times read 2d or 3d point (or worse, arrays of coefficients). nice able modify lexical_cast handle these cases.
as such, isn't specific opencv, chose simplest datatype.. i'm more interested in general solution.
edit 2
here's sample app i've been trying:
#include <opencv2/opencv.hpp> #include <boost/lexical_cast.hpp> template <typename t> std::istream& operator>>(std::istream& stream, cv::point_<t> &p) { // go here // set stream p } int main(int argc, char **argv) { cv::point_<float> p = boost::lexical_cast<cv::point_<float>>(std::string("1,2")); std::cout << "p = " << p << std::endl; homecoming 0; }
and fails beautiful c++ template error so:
in file included /home/rhand/development/mlx/ml_3rdparty/install/boost/include/boost/lexical_cast.hpp:41:0, /home/rhand/development/experiments/lexical_cast/test.cc:2: /home/rhand/development/mlx/ml_3rdparty/install/boost/include/boost/lexical_cast.hpp: in instantiation of ‘struct boost::detail::deduce_target_char_impl<boost::detail::deduce_character_type_later<cv::point_<float> > >’: /home/rhand/development/mlx/ml_3rdparty/install/boost/include/boost/lexical_cast.hpp:415:89: required ‘struct boost::detail::deduce_target_char<cv::point_<float> >’ /home/rhand/development/mlx/ml_3rdparty/install/boost/include/boost/lexical_cast.hpp:674:92: required ‘struct boost::detail::lexical_cast_stream_traits<std::basic_string<char>, cv::point_<float> >’ /home/rhand/development/mlx/ml_3rdparty/install/boost/include/boost/lexical_cast.hpp:2363:19: required ‘static target boost::detail::lexical_cast_do_cast<target, source>::lexical_cast_impl(const source&) [with target = cv::point_<float>; source = std::basic_string<char>]’ /home/rhand/development/mlx/ml_3rdparty/install/boost/include/boost/lexical_cast.hpp:2543:50: required ‘target boost::lexical_cast(const source&) [with target = cv::point_<float>; source = std::basic_string<char>]’ /home/rhand/development/experiments/lexical_cast/test.cc:11:82: required here /home/rhand/development/mlx/ml_3rdparty/install/boost/include/boost/static_assert.hpp:31:45: error: static assertion failed: target type neither std::istream`able nor std::wistream`able # define boost_static_assert_msg( ... ) static_assert(__va_args__) ^ /home/rhand/development/mlx/ml_3rdparty/install/boost/include/boost/lexical_cast.hpp:388:13: note: in expansion of macro ‘boost_static_assert_msg’ boost_static_assert_msg((result_t::value || boost::has_right_shift<std::basic_istream<wchar_t>, t >::value), ^ make[2]: *** [cmakefiles/test.dir/test.cc.o] error 1 make[1]: *** [cmakefiles/test.dir/all] error 2 make: *** [all] error 2
you can define specialization of boost::lexical_cast types want convert.
toy example:
typedef struct { int x; int y; } point; namespace boost { template<> std::string lexical_cast(const point& arg) { homecoming "2,3"; } } int main () { std::cout << boost::lexical_cast<std::string>(point ()) << std::endl; }
prints 2,3
.
going string point requires bit more work, can see how it.
c++ opencv boost casting lexical-cast
Comments
Post a Comment