将套接字函数bind()与libcxx一起使用的编译代码失败 [英] Compiling code that uses socket function bind() with libcxx fails
问题描述
我正在使用新的libcxx库,并且我有一个代码调用套接字函数bind()
.问题是,当我键入using namespace std;
时,编译器会为以下代码给我一个错误:
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));
使用clang的错误(svn构建):
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));
我认为问题在于using namespace std;
将标头<functional>
中的功能std::bind()
从标头<functional>
带到范围(尽管标头不包括在内).当我使用使用整个命名空间std的第三方库时,我无法轻松地将类名更改为完全限定的名称.
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.
我想知道这是库实现中的问题,还是C ++ 11中是否有一些新规则可能会破坏使用bind()
的旧代码.任何对此的想法将不胜感激.
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.
谢谢
罗马
推荐答案
在库的实现中这不是问题. C ++ 11引入了自己的 std::bind
namespace std
中的strong>函数,该函数用于将参数绑定到函数并支持一些高阶编程.
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.
具有namespace std
的原因是为了帮助防止新的库函数和类引起对现有代码的重大更改.这样做的原因是,所有内容的名称都以std::
开头,这样可以防止名称冲突.
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.
但是,如果您在程序中编写using namespace std;
,则您将面临这种潜在的重大变化,因为自由功能bind
和功能std::bind
不一定能消除歧义.
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.
要解决此问题,可以将bind
作为::bind
调用,以表明它位于全局名称空间中,或者可以删除程序顶部的using namespace std;
.
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.
希望这会有所帮助!
这篇关于将套接字函数bind()与libcxx一起使用的编译代码失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!