Compiling code that uses socket function bind() with libcxx fails

Go To StackoverFlow.com

12

I am using the new libcxx library and I have a code that calls the socket function bind(). The problem is that when I type using namespace std; the compiler gives me an error for the following code:

int res = bind(sockfd, (struct sockaddr *)&myAddr, sizeof(myAddr));

The error using clang (svn build):

error: no viable conversion from '__bind<int &, sockaddr *, unsigned long>' to 'int'
 int res = bind(sockfd, (struct sockaddr *)&myAddr, sizeof(myAddr));

I think that the problem is that using namespace std; brings the function std::bind() from the header <functional> to the scope (although the header is not included). As I am using a third party library that uses the entire namespace std I can't easily change the class names to fully qualified names.

I was wondering whether this is a problem in the implementation of the library or whether there are some new rules in C++11 that might potentially break an old code that uses bind(). Any thoughts on this would be appreciated.

Thanks

Roman

2012-04-05 20:10
by Roman Kutlak
What do you mean when you say that the third party library uses the entire namespace std - Troubadour 2012-04-05 20:26
I mean that it has using namespace std; in some of the files - Roman Kutlak 2012-04-05 20:43
Presumably in the source files only and not in the headers? That should not affect you at all - Troubadour 2012-04-05 21:40
@Troubadour : Presumably in the headers, otherwise he wouldn't be affected. Personally, I'd just shoot the library author who did that. :- - ildjarn 2012-04-05 22:08
@ildjarn: "The problem is that when I type using namespace std;" suggests not. I'm trying to ascertain why the OP thinks he "can't easily change the class names to fully qualified names.". I just don't understand that sentence - Troubadour 2012-04-06 22:34
I was talking about editing the source of the library actually, but that does not matter. Problem is that I would have to change the names every time I upgrade to a new version of the lib or persuade the lib author to do it himself. But as templatetypedef provided me with a simple solution (one single change) the library author was ok with that - Roman Kutlak 2012-04-06 22:47


32

This isn't a problem in the implementation of the libraries. C++11 introduced its own std::bind function into namespace std, which is used to bind parameters to functions and support a bit of higher-order programming.

The reason for having namespace std is to help prevent new library functions and classes from causing breaking changes in existing code. The reason for this is that everything has a name starting with std::, which prevents name collisions.

However, if you write using namespace std; in your program, you're exposing yourself to potential breaking changes like this one, since the free function bind and the function std::bind can't necessarily be disambiguated.

To fix this, you can call bind as ::bind to make clear that it's in the global namespace, or you can remove the using namespace std; at the top of the program.

Hope this helps!

2012-04-05 20:23
by templatetypedef
Thanks a lot.. it's working great! - RajibTheKing 2015-11-02 13:04
Finally I understood whats going on with bind not returning int - viktike 2017-03-20 19:55
Ads