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.

Colin Basnett
  • 4,052
  • 2
  • 30
  • 49

1 Answers1

13

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

sehe
  • 374,641
  • 47
  • 450
  • 633
  • 2
    I tend to say **["it's a hyperlink"](http://stackoverflow.com/questions/8111677/what-is-argument-dependent-lookup-aka-adl-or-koenig-lookup/8111750#8111750)** – sehe Nov 18 '14 at 08:50
  • Edited my post eliminating the `mandala` namespace I had previously. Same error occurs. – Colin Basnett Nov 18 '14 at 08:52
  • @cmbasnett **[Live On Coliru](http://coliru.stacked-crooked.com/a/01278169168ab3f3)** I'm pretty sure there is an observation error involved (or terribly broken compilers) – sehe Nov 18 '14 at 08:54
  • Directly copy-pasting that code on Coliru into Visual Studio 2013 and compiling it yields the same error. :S – Colin Basnett Nov 18 '14 at 09:00
  • im running VS 2013 too, and it compiles for me. Do you have update 4? – sp2danny Nov 18 '14 at 09:03
  • @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](http://coliru.stacked-crooked.com/a/9f1184af25ff7526)** though) – sehe Nov 18 '14 at 09:03
  • http://i.imgur.com/5KgTGxO.png (It is pathethic how MSVC takes well over a minute to compile that on my box though o.O) – sehe Nov 18 '14 at 09:10
  • @sp2danny Installing Update 4 now. – Colin Basnett Nov 18 '14 at 09:12
  • @cmbasnett That's not required. I run v120 (RTM). You have likely just not spotted that you got a different error. – sehe Nov 18 '14 at 09:15