I'm working on a library in C++ that is sort of a higher-level socket interface. The idea is to have similar calls with the same function names, but with a cleaner interface and better type safety. That means that I have my own (networking) bind function, which has a signature something like:
void MySocketLib::bind(const MySocketLib::MySocket &s, const MySocketLib::MyEndpoint &e);
Where MySocketLib is a namespace. This all works fine, so long as you call
MySocketLib::bind(s,e);
When s and e have the matching types. But, if you do something like this:
#include <functional>
#include "mysocketlib.h"
using namespace std;
using namespace MySocketLib;
...
bind(s,e);
Then, the bind picks up the std::bind in functional. I can always just call as MySocketLib::bind(s,e), but I would prefer if the un-qualified call picked up the MySocketLib::bind function instead of the std::bind template. This is using gnu g++.
It seems a similar problem between std::bind and ::bind has been addressed in the gcc headers. But it doesn't fix my problem, since my sockets are not ints.
What I've tried is to create a specialization, like this:
namespace std {
template<>
void bind(const MySocketLib::MySocket &s, const MySocketLib::MyEndpoint &e) {
MySocketLib::bind(s,a);
} }
But it tells me that my bind(...) does not match any template declaration. (Full compiler message below). Even though these are the exact types as in the call from which it happily instantiates std::bind. What I would like is either some advice on how on earth I can specialize std::bind, or some other way to accomplish making the plain bind() refer to MySocketLib::bind instead of std::bind when using both namespaces.
Following is the full compiler diagnostic from g++ for my failing specialization. Any advice appreciated.
In file included from mysocketlib.cpp:12:0:
mysocketlib.h:330:6: error: template-id 'bind<>' for 'void std::bind(const MySocketLib::MySocket&, const MySocketLib::MyEndpoint&)' does not match any template declaration
void bind(const MySocketLib::MySocket &s, const MySocketLib::MyEndpoint &a) {
^~~~
In file included from mysocketlib.h:323:0,
from mysocketlib.cpp:12:
/usr/include/c++/7/functional:899:5: note: candidates are: template<class _Result, class _Func, class ... _BoundArgs> typename std::_Bindres_helper<_Result, _Func, _BoundArgs>::type std::bind(_Func&&, _BoundArgs&& ...)
bind(_Func&& __f, _BoundArgs&&... __args)
^~~~
/usr/include/c++/7/functional:875:5: note: template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...)
bind(_Func&& __f, _BoundArgs&&... __args)
^~~~
Aucun commentaire:
Enregistrer un commentaire