Problem: Sending string literals is awkward

Solution: A function str_buffer specifically for
creating buffers for null terminated string literals.
This commit is contained in:
Gudmundur Adalsteinsson
2019-09-03 14:06:19 +00:00
parent 3b1038d035
commit 13cc1e0fe9
2 changed files with 27 additions and 0 deletions

13
zmq.hpp
View File

@@ -1110,6 +1110,19 @@ const_buffer buffer(std::basic_string_view<T, Traits> data, size_t n_bytes) noex
}
#endif
// Buffer for a string literal (null terminated)
// where the buffer size excludes the terminating character.
// Equivalent to zmq::buffer(std::string_view("...")).
template<class Char, size_t N>
const_buffer str_buffer(const Char (&data)[N]) noexcept
{
static_assert(detail::is_pod_like<Char>::value, "Char must be POD");
static_assert(N > 0, "N > 0");
assert(data[N - 1] == Char{0});
return const_buffer(N == 1 ? nullptr : static_cast<const Char*>(data),
(N - 1) * sizeof(Char));
}
#endif // ZMQ_CPP11
namespace detail