3

I'm currently programming a tcp/ip server using WSA. After some troubleshooting a friend of mine said that i should use bool __cdecl winsock_server ( void ) instead of bool winsock_server().

But he didn't explain to me what __cdecl and (void) are doing. I already know that __cdecl changes the way how arguments are put on the stack on assembler level, but what does (void) mean?

I should point out that I'm new to C++. I only programmed in C# and VB.NET before.

Thanks in advance!

melpomene
  • 84,125
  • 8
  • 85
  • 148
AcrimEx
  • 57
  • 1
  • 1
  • 8
  • 3
    They do nothing useful. `__cdecl` is already the default calling convention for free functions, in C++ member functions are `__thiscall` and cannot be `__cdecl`. Do not confuse it with `extern "C"` like the upvoted answer did. `(void)` is only useful in C to emphasize that the function has zero arguments. – Hans Passant Jun 06 '19 at 06:56

2 Answers2

12

__cdecl
You got it right. It enforces function calling convention to c-style and thus the way function is called (How arguments are passed, who cleans stack). BTW this is already default calling convention.

(void) v/s ()
In C++ they are equivalent(no arguments).
In C however former means no arguments and latter means any number of arguments. So it can cause problems when you reuse the header file for C.

Gyapti Jain
  • 4,056
  • 20
  • 40
  • do i get any advantages when using __cdecl ? – AcrimEx Jun 06 '19 at 06:21
  • @Th3Bamb00zl3r Yes, you get compatibility with 'C'. About the performance, there may be arguments on either side. (cdecl stands for c declaration) – Gyapti Jain Jun 06 '19 at 06:23
  • cdecl ain't extern C, see https://learn.microsoft.com/en-us/cpp/cpp/cdecl?view=vs-2019 – JVApen Jun 06 '19 at 07:23
  • @JVApen `__cdecl` is C calling convention. Both C and C++ symbols(functions and variable names) may use it. `extern C` is a C++ thing that suggests C++ compiler not to do name-mangling in C++ way. (C way is just an underscore before function name which is enough as function overloading is not supported). C++ declaration may use both or one or none according to the requirements. – Gyapti Jain Jun 06 '19 at 09:14
0

(void) means it has no arguments. It's more of a code style thing. you could use only () but writing (void) explicitly shows that the function have no arguments.