mirror of
https://github.com/zeromq/cppzmq.git
synced 2024-12-12 10:33:52 +01:00
Problem: message_t ctor for ranges too greedy
Solution: Detect ranges with enable_if idiom
This commit is contained in:
parent
d25c58a05d
commit
09ab20801a
@ -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;
|
||||
@ -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);
|
||||
|
@ -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);
|
||||
|
43
zmq.hpp
43
zmq.hpp
@ -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;
|
||||
|
||||
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user