0

是否可以扩展boost::lexical_cast以处理其他数据类型,而无需实际修改这些类?

在我的例子中,我想扩展它来处理像cv::Pointand之类的东西cv::Point3,获取一个字符串分隔的坐标列表并加载它们。所以能够执行以下操作:

cv::Point mypoint = boost::lexical_cast<cv::Point>("2,4");

该类已经具有流运算符,但与andcv::Point不兼容,因此它失败了。istreamwstream

编辑

我问这个是因为我在一个带有模板函数的框架中工作,该函数get_parameter用于boost::lexical_cast将字符串(从配置文件中读取)转换为所需的数据类型。它适用于整数和浮点数,但现在我必须多次调用它才能读取 2D 或 3D 点(或更糟糕的是,系数数组)。如果能够修改 lexical_cast 来处理这些情况,那就太好了。

因此,这不是 OpenCV 特有的,我只是选择了它作为最简单的数据类型。我对通用解决方案更感兴趣。

编辑 2

这是我一直在尝试的示例应用程序:

#include <opencv2/opencv.hpp>
#include <boost/lexical_cast.hpp>

template <typename T>
std::istream& operator>>(std::istream& stream, cv::Point_<T> &p) {
   // Eventually something will go here
   // to put stream into 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;
  return 0;
}

它失败了,出现了一个漂亮的 C++ 模板错误,如下所示:

In file included from /home/rhand/Development/mlx/ml_3rdparty/install/boost/include/boost/lexical_cast.hpp:41:0,
             from /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 from ‘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 from ‘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 from ‘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 from ‘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 from here
/home/rhand/Development/mlx/ml_3rdparty/install/boost/include/boost/static_assert.hpp:31:45: error: static assertion failed: Target type is 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
4

2 回答 2

1

如果记忆正确,您应该能够定义一个将使用的自由函数 ( istream& operator>>(istream&, cv::Point&)),lexical_cast只要它是在 lexical_cast 调用之前定义的。

编辑:请参阅重载 istream 运算符>> c++示例

编辑 2:这是一个工作示例(VS2013,boost 1.44 [是的,我需要更新!])。您能否发布一个在您的情况下失败的最小示例?

#include <iostream>
#include <boost/lexical_cast.hpp>

template <typename ElementType>
struct Point
{
    Point() : x(0), y(0) {}
    ElementType x, y;
};

template <typename ElementType>
std::istream& operator>>(std::istream& stream, Point<ElementType> &p)
{
    stream >> p.x;
    stream.get();
    stream >> p.y;
    return stream;
}

template <typename ElementType>
std::ostream& operator<<(std::ostream& stream, const Point<ElementType> &p)
{
    stream << p.x << "," << p.y;
    return stream;
}

int main(int argc, char **argv)
{
    Point<int> p = boost::lexical_cast<Point<int>>("1,2");
    std::cout << "p=[" << p << "]";
    std::cin.get();
    return 0;
}

编辑 3:使Point课程成为模板课程,因为这似乎是您的情况

编辑 4:使用 OpenCV 2.4.10,也适用于上述设置:

#include <iostream>
#include <boost/lexical_cast.hpp>
#include <opencv2/opencv.hpp>

template <typename ElementType>
std::istream& operator>>(std::istream& stream, cv::Point_<ElementType> &p)
{
    stream >> p.x;
    stream.get();
    stream >> p.y;
    return stream;
}

int main(int argc, char **argv)
{
    auto cv_p = boost::lexical_cast<cv::Point_<float>>(std::string("1,2"));
    std::cout << "opencv p=" << cv_p << "";
    std::cin.get();
    return 0;
}
于 2014-10-08T15:24:22.213 回答
1

您可以为要转换的类型定义 boost::lexical_cast 的特化。

玩具示例:

typedef struct { int x; int y; } Point;

namespace boost {
    template<>
      std::string lexical_cast(const Point& arg) { return "2,3"; }
}

int main () {
    std::cout << boost::lexical_cast<std::string>(Point ()) << std::endl;
    }

打印2,3

从一个字符串到一个点需要更多的工作,但你可以看到如何去做。

于 2014-10-08T18:22:13.200 回答