boost::lexical_cast not recognizing overloaded istream operator
Asked Answered
W

1

9

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.

Wildman answered 18/11, 2014 at 8:32 Comment(1)
Wasted my day dealing with this ;(Lysippus
U
12

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

Unconventional answered 18/11, 2014 at 8:44 Comment(9)
I tend to say "it's a hyperlink"Unconventional
Edited my post eliminating the mandala namespace I had previously. Same error occurs.Wildman
@cmbasnett Live On Coliru I'm pretty sure there is an observation error involved (or terribly broken compilers)Unconventional
Directly copy-pasting that code on Coliru into Visual Studio 2013 and compiling it yields the same error. :SWildman
im running VS 2013 too, and it compiles for me. Do you have update 4?Incase
@cmbasnett I have figured out the other error in your implementation, see updated answer. I've tested this on VS2013 too (see it Live On Coliru though)Unconventional
i.imgur.com/5KgTGxO.png (It is pathethic how MSVC takes well over a minute to compile that on my box though o.O)Unconventional
@Incase Installing Update 4 now.Wildman
@cmbasnett That's not required. I run v120 (RTM). You have likely just not spotted that you got a different error.Unconventional

© 2022 - 2024 — McMap. All rights reserved.