如何让 Boost.LexicalCast 发挥作用?

发布于 2024-12-11 18:28:25 字数 2383 浏览 0 评论 0原文

我在使用 boost::lexical_cast 时遇到问题。我正在尝试在 GLM(OpenGL 数学)库中的一个类上使用它。

为了允许词法转换,我实现了operator<<有问题的类的函数:

template <class T>
std::ostream& operator<<(std::ostream& out, const glm::detail::tvec2<T>& vec)
{
  out << vec.x << " " << vec.y;
  return out;
}

template <class T>
std::istream& operator>>(std::istream& in, glm::detail::tvec2<T>& vec)
{
  in >> vec.x;
  in >> vec.y;
  return in;
}

我测试了这样的运算符:

std::cout <<  glm::ivec2(1, 1) << glm::vec2(1.0f, 1.0f);

and:

std::stringstream ss("640 480");
glm::ivec2 pt;
ss >> pt;
std::cout << pt << std::endl;

这工作得很好,但是如果我尝试这样做:

glm::ivec2 pt = boost::lexical_cast<glm::ivec2>("1 1");

我收到以下错误:

/usr/include/boost/lexical_cast.hpp: In member function ‘bool boost::detail::lexical_stream_limited_src<CharT, Base, Traits>::operator>>(InputStreamable&) [with InputStreamable = glm::detail::tvec2<int>, CharT = char, Base = std::basic_streambuf<char>, Traits = std::char_traits<char>]’:
/usr/include/boost/lexical_cast.hpp:1151:13:   instantiated from ‘Target boost::detail::lexical_cast(typename boost::call_traits<B>::param_type, CharT*, std::size_t) [with Target = glm::detail::tvec2<int>, Source = const char*, bool Unlimited = false, CharT = char, typename boost::call_traits<B>::param_type = const char* const, std::size_t = long unsigned int]’
/usr/include/boost/lexical_cast.hpp:1174:77:   instantiated from ‘Target boost::lexical_cast(const Source&) [with Target = glm::detail::tvec2<int>, Source = char [8]]’
test2.cpp:41:59:   instantiated from here
/usr/include/boost/lexical_cast.hpp:785:29: error: cannot bind ‘std::basic_istream<char>’ lvalue to ‘std::basic_istream<char>&&’
/usr/include/c++/4.6/istream:852:5: error:   initializing argument 1 of ‘std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&&, _Tp&) [with _CharT = char, _Traits = std::char_traits<char>, _Tp = glm::detail::tvec2<int>]’

编辑:似乎只有在标头时才会发生错误Boost.PropertyTree 都包含在内。

I'm having issues with boost::lexical_cast. I am trying to use it on a class from the GLM (OpenGL Mathematics) library.

To allow for lexical casting, I have implemented operator<< functions for the class in question:

template <class T>
std::ostream& operator<<(std::ostream& out, const glm::detail::tvec2<T>& vec)
{
  out << vec.x << " " << vec.y;
  return out;
}

template <class T>
std::istream& operator>>(std::istream& in, glm::detail::tvec2<T>& vec)
{
  in >> vec.x;
  in >> vec.y;
  return in;
}

I tested the operators like so:

std::cout <<  glm::ivec2(1, 1) << glm::vec2(1.0f, 1.0f);

and:

std::stringstream ss("640 480");
glm::ivec2 pt;
ss >> pt;
std::cout << pt << std::endl;

This works just fine, however if I try this:

glm::ivec2 pt = boost::lexical_cast<glm::ivec2>("1 1");

I get the following error:

/usr/include/boost/lexical_cast.hpp: In member function ‘bool boost::detail::lexical_stream_limited_src<CharT, Base, Traits>::operator>>(InputStreamable&) [with InputStreamable = glm::detail::tvec2<int>, CharT = char, Base = std::basic_streambuf<char>, Traits = std::char_traits<char>]’:
/usr/include/boost/lexical_cast.hpp:1151:13:   instantiated from ‘Target boost::detail::lexical_cast(typename boost::call_traits<B>::param_type, CharT*, std::size_t) [with Target = glm::detail::tvec2<int>, Source = const char*, bool Unlimited = false, CharT = char, typename boost::call_traits<B>::param_type = const char* const, std::size_t = long unsigned int]’
/usr/include/boost/lexical_cast.hpp:1174:77:   instantiated from ‘Target boost::lexical_cast(const Source&) [with Target = glm::detail::tvec2<int>, Source = char [8]]’
test2.cpp:41:59:   instantiated from here
/usr/include/boost/lexical_cast.hpp:785:29: error: cannot bind ‘std::basic_istream<char>’ lvalue to ‘std::basic_istream<char>&&’
/usr/include/c++/4.6/istream:852:5: error:   initializing argument 1 of ‘std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&&, _Tp&) [with _CharT = char, _Traits = std::char_traits<char>, _Tp = glm::detail::tvec2<int>]’

EDIT: It seems the error only occurs if headers for Boost.PropertyTree are included.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水波映月 2024-12-18 18:28:25

您发布了此提取运算符:

template <class T>
std::istream& operator>>(std::istream& in, glm::detail::tvec2<T>& vec)

但错误意味着它正在尝试使用以下内容进行编译:

std::istream& std::operator>>(std::istream&&, _Tp&)
[with ... _Tp = glm::detail::tvec2<int>]

您确定您发布的代码与您正在编译的代码相匹配吗?
(如果您不能立即看到它,请关注运算符的第一个参数)。


好的,这个完整的代码对我来说效果很好:

#include <sstream>
#include <iostream>

#include <boost/lexical_cast.hpp>

template <class T>
struct tvec2 { 
    tvec2() : x(), y() {}
    tvec2(tvec2 const &) = default;
    tvec2(T x_, T y_) : x(x_), y(y_) {}

    T x;
    T y;
};

template <class T>
std::ostream& operator<<(std::ostream& out, const tvec2<T>& vec)
{
  out << vec.x << " " << vec.y;
  return out;
}

template <class T>
std::istream& operator>>(std::istream& in, tvec2<T>& vec)
{
    // yuck, boost disables skipws on the input stream
    in >> vec.x;
    if (in.good() && in.ignore(256, ' ').good())
        in >> vec.y;
    return in;
}

void test_operators()
{
    tvec2<int> intvec(2,3);
    std::cout << "intvec = {" << intvec << "}\n";

    std::stringstream ss;
    ss << intvec;

    tvec2<int> dupvec;
    ss >> dupvec;
    std::cout << "dupvec = {" << dupvec << "}\n";
}

void test_lexical_cast()
{
    std::cout << "and now with lexical_cast ...\n";
    tvec2<int> dupvec = boost::lexical_cast<tvec2<int> >("2 3");
    std::cout << "dupvec = {" << dupvec << "}\n";
}

int main()
{
    test_operators();
    test_lexical_cast();
}

You posted this extraction operator:

template <class T>
std::istream& operator>>(std::istream& in, glm::detail::tvec2<T>& vec)

but the error implies it is trying to compile with this:

std::istream& std::operator>>(std::istream&&, _Tp&)
[with ... _Tp = glm::detail::tvec2<int>]

Are you sure the code you posted matches what you're compiling?
(Focus on the first argument to the operator if you can't see it right away).


OK, this complete code works fine for me:

#include <sstream>
#include <iostream>

#include <boost/lexical_cast.hpp>

template <class T>
struct tvec2 { 
    tvec2() : x(), y() {}
    tvec2(tvec2 const &) = default;
    tvec2(T x_, T y_) : x(x_), y(y_) {}

    T x;
    T y;
};

template <class T>
std::ostream& operator<<(std::ostream& out, const tvec2<T>& vec)
{
  out << vec.x << " " << vec.y;
  return out;
}

template <class T>
std::istream& operator>>(std::istream& in, tvec2<T>& vec)
{
    // yuck, boost disables skipws on the input stream
    in >> vec.x;
    if (in.good() && in.ignore(256, ' ').good())
        in >> vec.y;
    return in;
}

void test_operators()
{
    tvec2<int> intvec(2,3);
    std::cout << "intvec = {" << intvec << "}\n";

    std::stringstream ss;
    ss << intvec;

    tvec2<int> dupvec;
    ss >> dupvec;
    std::cout << "dupvec = {" << dupvec << "}\n";
}

void test_lexical_cast()
{
    std::cout << "and now with lexical_cast ...\n";
    tvec2<int> dupvec = boost::lexical_cast<tvec2<int> >("2 3");
    std::cout << "dupvec = {" << dupvec << "}\n";
}

int main()
{
    test_operators();
    test_lexical_cast();
}
听,心雨的声音 2024-12-18 18:28:25

答案已被其他人找到,所以我将其留在这里。

他提到了一些关于参数依赖查找的内容,并建议将流运算符放置在 glm::detail 命名空间中,如下所示:

namespace glm {
namespace detail {

template <class T>
std::ostream& operator<<(std::ostream& out, const glm::detail::tvec2<T>& vec)
{
    out << vec.x << " " << vec.y;
    return out;
}

template <class T>
std::istream& operator>>(std::istream& in, glm::detail::tvec2<T>& vec)
{
    in >> vec.x;
    if (in.good() && in.ignore(256, ' ').good())
        in >> vec.y;
    return in;
}

}} // glm::detail

之后,一切正常。

The answer was found by another, so I'll leave it here.

He mentioned something about argument dependent lookup, and suggested that the stream operators be placed in the glm::detail namepace like so:

namespace glm {
namespace detail {

template <class T>
std::ostream& operator<<(std::ostream& out, const glm::detail::tvec2<T>& vec)
{
    out << vec.x << " " << vec.y;
    return out;
}

template <class T>
std::istream& operator>>(std::istream& in, glm::detail::tvec2<T>& vec)
{
    in >> vec.x;
    if (in.good() && in.ignore(256, ' ').good())
        in >> vec.y;
    return in;
}

}} // glm::detail

After that, everything works fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文