简体   繁体   English

这个C ++功能的名称是什么?

[英]What is the name of this C++ functionality?

I was writing some C++ code and mistakenly omitted the name of a function WSASocket . 我正在编写一些C ++代码,并错误地省略了函数WSASocket的名称。 However, my compiler did not raise an error and associated my SOCKET with the integer value 1 instead of a valid socket. 但是,我的编译器没有引发错误并将我的SOCKET与整数值1而不是有效套接字相关联。

The code in question should have looked like this: 有问题的代码应该是这样的:

this->listener = WSASocket(address->ai_family, address->ai_socktype, address->ai_protocol, NULL, NULL, WSA_FLAG_OVERLAPPED);

But instead, it looked like this: 但相反,它看起来像这样:

this->listener = (address->ai_family, address->ai_socktype, address->ai_protocol, NULL, NULL, WSA_FLAG_OVERLAPPED);

Coming from other languages, this looks like it may be some kind of anonymous type. 来自其他语言,这看起来可能是某种匿名类型。 What is the name of the feature, in the case it is really a feature? 该功能的名称是什么,如果它真的是一个功能?

What is its purpose? 它的目的是什么?

It's difficult to search for it, when you don't know where to begin. 当你不知道从哪里开始时,很难搜索它。

The comma operator† evaluates the left hand side, discards its value, and as a result yields the right hand side. 逗号运算符†评估左侧,丢弃其值,结果产生右侧。 WSA_FLAG_OVERLAPPED is 1, and that is the result of the expression; WSA_FLAG_OVERLAPPED为1,这是表达式的结果; all the other values are discarded. 所有其他值都被丢弃。 No socket is ever created. 没有创建套接字。


† Unless overloaded. †除非超载。 Yes, it can be overloaded. 是的,它可以超载。 No, you should not overload it. 不,你不应该超载它。 Step away from the keyboard, right now! 现在就远离键盘!

The comma operator is making sense of your code. 逗号运算符正在理解您的代码。

You are effectively setting this->listener = WSA_FLAG_OVERLAPPED; 你有效地设置this->listener = WSA_FLAG_OVERLAPPED; which just happens to be syntatically valid. 这恰好是合成有效的。

The compiler is evaluating each sequence point in turn within the parenthesis and the result is the final expression, WSA_FLAG_OVERLAPPED in the expression. 编译器在括号内依次评估每个序列点 ,结果是表达式中的最终表达式WSA_FLAG_OVERLAPPED

The comma operator , is a sequence point in C++. 逗号运算符,是在C ++中的序列点。 The expression to the left of the comma is fully evaluated before the expression to the right is. 在右边的表达式之前,将完全评估逗号左侧的表达式。 The result is always the value to the right. 结果始终是右边的值。 When you've got an expression of the form (x1, x2, x3, ..., xn) the result of the expression is always xn . 当你有一个表达式(x1,x2,x3,...,xn)时,表达式的结果总是为xn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM