Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lexical_cast doesn't recognize std::string_view or boost::string_view #30

Closed
pdimov opened this issue Oct 29, 2019 · 5 comments
Closed

Comments

@pdimov
Copy link
Member

pdimov commented Oct 29, 2019

As per title; lexical_cast has specializations for string types, but not for string view types. Reported on the C++ Slack.

@sehe
Copy link

sehe commented Feb 8, 2022

Is this still current? I have just used boost::convert::try_lexical_convert just fine with both std::string_view and boost::iterator_range<>, so it would seem this has been fixed somewhere before 1.78.0?

@pdimov
Copy link
Member Author

pdimov commented Feb 8, 2022

I think that it's still current, yes. Looking at https://godbolt.org/z/WMa7vhnT9, the function f1 that uses a string

int f1()
{
    std::string s1( "5" );
    return boost::lexical_cast<int>( s1 );
}

goes directly to boost::detail::lcast_ret_unsigned<std::char_traits<char>, unsigned int, char>::convert(), whereas the two functions using string views

int f2()
{
    std::string_view s1( "5" );
    return boost::lexical_cast<int>( s1 );
}

int f3()
{
    boost::string_view s1( "5" );
    return boost::lexical_cast<int>( s1 );
}

take the generic path using a stream.

Similarly, in https://godbolt.org/z/9W6M1c35e, f1

std::string f1()
{
    std::string s1( "5" );
    return boost::lexical_cast<std::string>( s1 );
}

does a straight string copy, whereas f2 and f3

std::string f2()
{
    std::string_view s1( "5" );
    return boost::lexical_cast<std::string>( s1 );
}

std::string f3()
{
    boost::string_view s1( "5" );
    return boost::lexical_cast<std::string>( s1 );
}

go through the slow stream path.

@sehe
Copy link

sehe commented Feb 8, 2022

Ah. I completely misunderstood the problem description then. It's not that the conversions aren't supported, it's that they don't generate optimal code. The negative examples might actually be good addition for the original description.

@pdimov
Copy link
Member Author

pdimov commented Feb 8, 2022

Edited to supply the code examples in case the CE links disappear.

@apolukhin
Copy link
Member

apolukhin commented Feb 4, 2024

Merged to master branch.

Many thanks for the bug report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@sehe @apolukhin @pdimov and others