Jump to content
  • Advertisement
Sign in to follow this  
Chris_F

Boost Lexical Cast

This topic is 2605 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);


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>]’

Share this post


Link to post
Share on other sites
Advertisement
Are you using the latest version of the boost library? If I'm reading the error message correctly, that looks like your compiler uses C++11 features, but your version of boost doesn't play nicely with rvalue references.

Share this post


Link to post
Share on other sites
I have Boost 1.46, which is the latest version available in the Debian repositories.

EDIT: I even downloaded 1.47 and gave it a try.

Share this post


Link to post
Share on other sites
It only seems to happen if I include any Boost.PropertyTree header files.

I removed these:


#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ptree_serialization.hpp>
#include <boost/property_tree/json_parser.hpp>


and it seems to compile.

Since my goal is to use glm::tvec<T> in a property tree, this is obviously an issue.

Share this post


Link to post
Share on other sites
The solution was to put the stream operators into the glm::detail namespace.


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!