Adding type for conversion by boost::lexical_cast (segfault)

user13271303

I want to convert my type back and forth between string representation and actual data using boost::lexical_cast. This is what I'm trying to do:

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

struct Foo
{
    Foo() = default;

    Foo(const std::string& str)
        : str(str)
    {
    }

    friend std::istream& operator>>(std::istream& is, Foo& foo);
    friend std::ostream& operator<<(std::ostream& os, const Foo& foo);

    private:
        std::string str;
};

// For conversion to Foo.
std::istream& operator>>(std::istream& is, Foo& foo)
{
    return is >> foo.str;
}

// For conversion from Foo.
std::ostream& operator<<(std::ostream& os, const Foo& foo)
{
    return os << foo.str;
}

int main()
{
    boost::lexical_cast<Foo>("ok");
    boost::lexical_cast<std::string>(Foo("ok"));
}

But this results in a segfault. Why?


[Documentation](https://www.boost.org/doc/libs/1_73_0/doc/html/boost_lexical_cast.html)
Arthur Tacca

It sounds like the crash is due to a stack overflow. I think your operator>> is calling itself, by implicitly converting foo.str back into a Foo.

Try removing the implicit conversion by making that constructor explicit:

explicit Foo(const std::string& str) noexcept
    : str(str)
{
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Check if type can be an argument to boost::lexical_cast<string>

boost::lexical_cast wrong output

Catching overflows with boost::lexical_cast

Strange exception from boost::lexical_cast with boost::filesystem

boost::lexical_cast can convert hex inside string to int?

boost::lexical_cast<int> ("-138.8468953457983248") Throws exception

Boost's lexical_cast From double to string Precision

boost::lexical_cast not recognizing overloaded istream operator

When does boost::lexical_cast to std::string fail?

boost::optional and type conversion

Extracting number from file name with boost::filesystem and boost::lexical_cast

What's the difference between std::to_string, boost::to_string, and boost::lexical_cast<std::string>?

Boost compile error on converting UUID to string using boost::lexical_cast

Unexpected results from boost::lexical_cast<int> with boost::iterator_range

Is boost::lexical_cast redundant with c++11 stoi, stof and family?

Why does boost::lexical_cast throw an exception even though it converted the value?

Enable boost::lexical_cast to throw out of range error for values smaller than double range

boost bad lexical cast: source type value could not be interpreted as target when converting a string to unsigned long long

Boost Spirit Segfault In Parser

boost semaphore cause segfault

Boost::Regex Segfault (I think)

Boost ASIO segfault in release mode

lexical_cast strtof strtold lose accuracy?

Segfault after adding #pragma loop

C# type conversion: Explicit cast exists but throws a conversion error?

Is there a way to achieve implicit type cast or conversion in java

Unable to cast string to type with implicit conversion

Boost graph conversion error

Boost.Asio segfault, no idea why