Skip to content

Commit

Permalink
Problem: message_t ctor for ranges too greedy
Browse files Browse the repository at this point in the history
Solution: Detect ranges with enable_if idiom
  • Loading branch information
gummif committed May 15, 2019
1 parent d25c58a commit 09ab208
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
17 changes: 17 additions & 0 deletions tests/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ static_assert(std::is_nothrow_swappable<zmq::message_t>::value,
"message_t should be nothrow swappable");
#endif

#ifdef ZMQ_CPP11
TEST_CASE("range SFINAE", "[message]")
{
CHECK(!zmq::detail::is_range<int>::value);
CHECK(zmq::detail::is_range<std::string>::value);
CHECK(zmq::detail::is_range<std::string&>::value);
CHECK(zmq::detail::is_range<const std::string&>::value);
CHECK(zmq::detail::is_range<decltype("hello")>::value);
}
#endif

TEST_CASE("message default constructed", "[message]")
{
const zmq::message_t message;
Expand Down Expand Up @@ -48,6 +59,12 @@ TEST_CASE("message constructor with iterators", "[message]")
CHECK(0 == memcmp(data, hi_msg.data(), 2));
}

TEST_CASE("message constructor with size", "[message]")
{
const zmq::message_t msg(5);
CHECK(msg.size() == 5);
}

TEST_CASE("message constructor with buffer and size", "[message]")
{
const std::string hi(data);
Expand Down
2 changes: 1 addition & 1 deletion tests/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ TEST_CASE("socket send recv message_t", "[socket]")
s2.bind("inproc://test");
s.connect("inproc://test");

zmq::message_t smsg(size_t{10});
zmq::message_t smsg(10);
const auto res_send = s2.send(smsg, zmq::send_flags::none);
CHECK(res_send);
CHECK(*res_send == 10);
Expand Down
43 changes: 39 additions & 4 deletions zmq.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,39 @@ typedef struct

namespace zmq
{

#ifdef ZMQ_CPP11
namespace detail
{
template<class T> using void_t = void;

template<class Iter>
using iter_value_t = typename std::iterator_traits<Iter>::value_type;

template<class Range>
using range_iter_t = decltype(
std::begin(std::declval<typename std::remove_reference<Range>::type &>()));

template<class Range>
using range_value_t = iter_value_t<range_iter_t<Range>>;

template<class T, class = void> struct is_range : std::false_type
{
};

template<class T>
struct is_range<
T,
void_t<decltype(
std::begin(std::declval<typename std::remove_reference<T>::type &>())
== std::end(std::declval<typename std::remove_reference<T>::type &>()))>>
: std::true_type
{
};

} // namespace detail
#endif

typedef zmq_free_fn free_fn;
typedef zmq_pollitem_t pollitem_t;

Expand Down Expand Up @@ -278,10 +311,12 @@ class message_t
}

#if defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11)
// TODO: this function is too greedy, must add
// SFINAE for begin and end support.
template<typename T>
explicit message_t(const T &msg_) : message_t(std::begin(msg_), std::end(msg_))
template<class Range,
typename = typename std::enable_if<
detail::is_range<Range>::value
&& std::is_trivially_copyable<detail::range_value_t<Range>>::value
&& !std::is_same<Range, message_t>::value>::type>
explicit message_t(const Range &rng) : message_t(std::begin(rng), std::end(rng))
{
}
#endif
Expand Down

0 comments on commit 09ab208

Please sign in to comment.