Support for string_view
Version of cppzmq : 4.10.0 Compiler version : 9.4.0 C++ version : 17 I mainly use std::string_view to handle string into my functions and I would like to be able to use them without conversion. Before :
std::string_view url = "url";
socket.connect(url.data());
After :
std::string_view url = "url";
socket.connect(url);
Here the error :
src/network/SubNetwork.cpp:11:24: error: no matching function for call to ‘zmq::socket_t::connect(std::string_view&)’
socket_.connect(url);
^
In file included from src/network/Network.h:5,
from src/network/SubNetwork.h:4,
from src/network/SubNetwork.cpp:1:
./cppzmq/zmq.hpp:1894:10: note: candidate: ‘void zmq::detail::socket_base::connect(const string&)’
void connect(std::string const &addr) { connect(addr.c_str()); }
^~~~~~~
./cppzmq/zmq.hpp:1894:10: note: no known conversion for argument 1 from ‘std::string_view’ {aka ‘std::basic_string_view<char>’} to ‘const string&’ {aka ‘const std::__cxx11::basic_string<char>&’}
./cppzmq/zmq.hpp:1896:10: note: candidate: ‘void zmq::detail::socket_base::connect(const char*)’
void connect(const char *addr_)
^~~~~~~
./cppzmq/zmq.hpp:1896:10: note: no known conversion for argument 1 from ‘std::string_view’ {aka ‘std::basic_string_view<char>’} to ‘const char*’
This is also a problem for bind or zmq::str_buffer. I would like to try to get it done if possible.
The libzmq function take null terminates strings so string views must be converted to strings. Your before example is buggy if the underlying string is not null terminated, use std::string(url) instead. str_buffer is for string literals only. Use zmq::buffer instead (I think that works).
Oh okay thanks but maybe it might be a good idea to force the good habits like :
void connect(std::string_view addr) { connect(std::string(addr)); }
Same for bind, disconnect, etc...