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

Safe version of std::string_view::copy() #961

Open
jtv opened this issue Mar 8, 2025 · 0 comments
Open

Safe version of std::string_view::copy() #961

jtv opened this issue Mar 8, 2025 · 0 comments

Comments

@jtv
Copy link
Owner

jtv commented Mar 8, 2025

This is a need that I keep running into. In C++26 we're finally getting std::span::at(), and that's nice, but where it really gets interesting is when I need to do a std::string_view::copy() into a std::span<char>. That's an opportunity for a bigger overrun, and there's a bulk discount to be had compared to checking at() for each element.

If I were writing this as a C++ Standard proposal, I'd probably ask for a std::expected return value, so the caller can decide what to do about an overrun without having to catch an exception. Or maybe just separate the bounds check from the actual copying. And then I'd agonise over still having to spell out the code for reporting overruns. It wouldn't save me all that much code.

But in libpqxx I simply want a wrapper for std::string_view::copy() that either returns a std::size_t, or throws a conversion_overrun.

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

1 participant