Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the name of this C++ functionality?

Tags:

c++

I was writing some C++ code and mistakenly omitted the name of a function WSASocket. However, my compiler did not raise an error and associated my SOCKET with the integer value 1 instead of a valid socket.

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.

like image 200
Michael J. Gray Avatar asked Nov 18 '14 10:11

Michael J. Gray


People also ask

What is a function name in C?

What type is a function name in C? A function name or function designator has a function type. When it is used in an expression, except when it is the operand of sizeof or & operator, it is converted from type "function returning type" to type "pointer to a function returning type".

Is there a this keyword in C?

In C you do not have the this keyword. Only in C++ and in a class, so your code is C and you use your this variable as a local method parameter, where you access the array struct.


2 Answers

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; 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!

like image 186
R. Martinho Fernandes Avatar answered Oct 13 '22 06:10

R. Martinho Fernandes


The comma operator is making sense of your code.

You are effectively setting this->listener = WSA_FLAG_OVERLAPPED; which just happens to be syntatically valid.

like image 45
Bathsheba Avatar answered Oct 13 '22 04:10

Bathsheba