Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::lexical_cast not recognizing overloaded istream operator

I have the following code:

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

struct vec2_t
{
    float x;
    float y;
};

std::istream& operator>>(std::istream& istream, vec2_t& v)
{
    istream >> v.x >> v.y;

    return istream;
}

int main()
{
    auto v = boost::lexical_cast<vec2_t>("1231.2 152.9");

    std::cout << v.x << " " << v.y;

    return 0;
}

I am receiving the following compile error from Boost:

Error 1 error C2338: Target type is neither std::istreamable nor std::wistreamable

This seems straightforward enough, and I have been hitting my head against the desk for the last hour. Any help would be appreciated!

EDIT: I am using Visual Studio 2013.

like image 584
Colin Basnett Avatar asked Nov 18 '14 08:11

Colin Basnett


People also ask

What is boost lexical cast in C++?

Boost.LexicalCast provides a cast operator, boost::lexical_cast, that can convert numbers from strings to numeric types like int or double and vice versa. boost::lexical_cast is an alternative to functions like std::stoi (), std::stod (), and std::to_string (), which were added to the standard library in C++11. Example 6.1.

How do I use the cast operator boost::lexical_cast?

The cast operator boost::lexical_cast can convert numbers of different types. Example 6.1 first converts the integer 123 to a string, then converts the string to a floating point number. To use boost::lexical_cast, include the header file boost/lexical_cast.hpp. boost::lexical_cast uses streams internally to perform the conversion.

What is the use of boost in C++?

The Boost C++ Libraries. Boost.LexicalCast provides a cast operator, boost::lexical_cast, that can convert numbers from strings to numeric types like int or double and vice versa. boost::lexical_cast is an alternative to functions like std::stoi(), std::stod(), and std::to_string(), which were added to the standard library in C++11.

How do I cast a string to a number in boost?

Boost.LexicalCast which is defined in the Library “boost/lexical_cast.hpp” provides a cast operator, boost::lexical_cast, that can convert numbers from strings to numeric types like int or double and vice versa.


1 Answers

There's 2-phase lookup at play.

You need to enable the overload using ADL, so lexical_cast will find it in the second phase.

So, you should move the overload into namespace mandala

Here's a completely fixed example (you should also use std::skipws):

Live On Coliru

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

namespace mandala
{
    struct vec2_t {
        float x,y;
    };    
}

namespace mandala
{
    std::istream& operator>>(std::istream& istream, vec2_t& v) {
        return istream >> std::skipws >> v.x >> v.y;
    }
}

int main()
{
    auto v = boost::lexical_cast<mandala::vec2_t>("123.1 15.2");
    std::cout << "Parsed: " << v.x << ", " << v.y << "\n";
}

enter image description here

like image 110
sehe Avatar answered Nov 15 '22 22:11

sehe