Skip to content

Commit

Permalink
Make more zview constructors noexcept. (jtv#625)
Browse files Browse the repository at this point in the history
This is a silly problem to have.  In a way it's good.  Sometimes gcc
warns (and in my development builds, warnings are errors!) that some
`zview` constructors throw no errors, and would I perhaps like to slap
a `noexcept` on them?

A _fine_ idea, were it not for the facts that (1) `pqxx::zview` extends
`std::string_view`, and (2) the corresponding `string_view` constructor
does not have the `noexcept`.  So... can I make that promise myself?

Turns out I can.  Looking at gcc's `string_view` implementation I see
that it does declare those constructors as `noexcept`.  All I had to do
was declare my constructors as "`noexcept` provided that `string_view`
is okay with it.")
  • Loading branch information
jtv committed Jan 14, 2023
1 parent e39c77a commit 142b920
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Use `array_parser` only on comma-separated types, i.e. most of them. (#590)
- Bumping requirements versions: need postgres 10.
- Fix `array_parser` bug when parsing semicolon in an unquoted string.
- Make some `zview` constructors `noexcept` if `string_view` does it.
- Faster text decoding in `stream_from`, and escaping in `stream_to`. (#601)
- At last, streaming throughput is faster (on my system) than a regular query.
- Deprecate `basic_fieldstream` and `fieldstream`.
Expand Down
9 changes: 6 additions & 3 deletions include/pqxx/zview.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ public:
constexpr zview() noexcept = default;

/// Convenience overload: construct using pointer and signed length.
constexpr zview(char const text[], std::ptrdiff_t len) :
constexpr zview(char const text[], std::ptrdiff_t len)
noexcept(noexcept(std::string_view{text, static_cast<std::size_t>(len)})) :
std::string_view{text, static_cast<std::size_t>(len)}
{}

/// Convenience overload: construct using pointer and signed length.
constexpr zview(char text[], std::ptrdiff_t len) :
constexpr zview(char text[], std::ptrdiff_t len)
noexcept(noexcept(std::string_view{text, static_cast<std::size_t>(len)})):
std::string_view{text, static_cast<std::size_t>(len)}
{}

Expand Down Expand Up @@ -73,7 +75,8 @@ public:
* do it many times, it's probably better to create the `zview` once and
* re-use it.
*/
constexpr zview(char const str[]) : std::string_view{str} {}
constexpr zview(char const str[]) noexcept(noexcept(std::string_view{str})) :
std::string_view{str} {}

/// Construct a `zview` from a string literal.
/** A C++ string literal ("foo") normally looks a lot like a pointer to
Expand Down

0 comments on commit 142b920

Please sign in to comment.