gpt4 book ai didi

c++ - 扩展 boost::lexical_cast 用于其他类数据类型

转载 作者:行者123 更新时间:2023-11-28 02:40:09 26 4
gpt4 key购买 nike

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

在我的例子中,我想扩展它来处理像 cv::Pointcv::Point3 这样的事情,采用字符串分隔的坐标列表并加载他们..所以有能力做这样的事情:

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

cv::Point 类已有流操作符,但与istreamwstream 不兼容,因此失败。

编辑

我问这个是因为我在一个框架中工作,该框架具有模板化函数 get_parameter,该函数使用 boost::lexical_cast 转换字符串(从配置文件读取)成所需的数据类型。它适用于整数和 float ,但现在我必须多次调用它来读取 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

最佳答案

您可以为要转换的类型定义 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

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

关于c++ - 扩展 boost::lexical_cast 用于其他类数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26260497/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com