From ab588fb7c932b55f25255e90ebab4ecfd90da54e Mon Sep 17 00:00:00 2001 From: Gudmundur Adalsteinsson Date: Wed, 11 Sep 2019 20:21:02 +0000 Subject: [PATCH] Assertion and constexpr improvements for str_buffer --- tests/buffer.cpp | 2 +- zmq.hpp | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/buffer.cpp b/tests/buffer.cpp index 0ed9e0b..6767f8d 100644 --- a/tests/buffer.cpp +++ b/tests/buffer.cpp @@ -244,7 +244,7 @@ TEST_CASE("const_buffer creation with str_buffer", "[buffer]") CHECK(b.data() == static_cast(wd)); zmq::const_buffer b2_null = zmq::buffer("hello"); - zmq::const_buffer b2 = zmq::str_buffer("hello"); + constexpr zmq::const_buffer b2 = zmq::str_buffer("hello"); CHECK(b2_null.size() == 6); CHECK(b2.size() == 5); CHECK(std::string(static_cast(b2.data()), b2.size()) == "hello"); diff --git a/zmq.hpp b/zmq.hpp index e26b273..ad6de8f 100644 --- a/zmq.hpp +++ b/zmq.hpp @@ -1114,12 +1114,13 @@ const_buffer buffer(std::basic_string_view data, size_t n_bytes) noex // where the buffer size excludes the terminating character. // Equivalent to zmq::buffer(std::string_view("...")). template -const_buffer str_buffer(const Char (&data)[N]) noexcept +constexpr const_buffer str_buffer(const Char (&data)[N]) noexcept { static_assert(detail::is_pod_like::value, "Char must be POD"); - static_assert(N > 0, "N > 0"); +#ifdef ZMQ_CPP14 assert(data[N - 1] == Char{0}); - return const_buffer(N == 1 ? nullptr : static_cast(data), +#endif + return const_buffer(static_cast(data), (N - 1) * sizeof(Char)); }