void argument

P

PengYu.UT

Hi,

I know that there is a difference (in C) between

int fun(void);

int fun();

The first one takes no arguments. The second can take any number of
arguments, including 0.

I'm wondering if this difference is still hold in C++.

Thanks,
Peng
 
J

Jacques Labuschagne

Hi,

I know that there is a difference (in C) between

int fun(void);

int fun();

The first one takes no arguments. The second can take any number of
arguments, including 0.

I'm wondering if this difference is still hold in C++.

Nope, they're exactly the same thing in C++: fun takes no arguments. For
a function taking any number of arguments use ellipses, e.g. void fun(...);

Jacques.
 
C

Cy Edmunds

Hi,

I know that there is a difference (in C) between

int fun(void);

int fun();

The first one takes no arguments. The second can take any number of
arguments, including 0.

I'm wondering if this difference is still hold in C++.

Thanks,
Peng

No, in C++ they mean the same: a function which takes no arguments. The
business about taking any number of arguments is disallowed in C++. Because
of overloading, the argument types are part of the function's signature.
 
N

Nan Li

No.

[nan@athena test]$ cat test2.c
void foo()
{
}
int main()
{
foo(1,2);
return 1;
}
[nan@athena test]$ g++ -Wall -x c test2.c
[nan@athena test]$ g++ -Wall -x c++ test2.c
test2.c: In function `int main()':
test2.c:2: error: too many arguments to function `void foo()'
test2.c:7: error: at this point in file
 
C

Chris Hills

Nan Li said:

No what?

If you are replying top something quote the message you are replying to.

If you are using the broken google interface to usenet please set the
options properly.
 
B

ben

Hi,

I know that there is a difference (in C) between

int fun(void);

int fun();

The first one takes no arguments. The second can take any number of
arguments, including 0.

I'm wondering if this difference is still hold in C++.

Thanks,
Peng


No. In C++, the above two prototypes are exactly the same.

Ben
 
C

Clark S. Cox III

Nope, they're exactly the same thing in C++: fun takes no arguments.
For a function taking any number of arguments use ellipses, e.g. void
fun(...);

Actually, no. You cannot define a function taking any number of
arguments that way. Using the ellipsis (even in C), requires at least
one non variadic parameter, i.e.:

/* This function requires at least one int argument, but can take any
number of additional, non-POD arguments */
void fun(int, ...)
{
}

/* This function definition is illegal:*/
void fun2(...)
{
}
 
A

Alf P. Steinbach

* Clark S. Cox III:
Actually, no. You cannot define a function taking any number of
arguments that way. Using the ellipsis (even in C), requires at least
one non variadic parameter, i.e.:

Not in C++ (I don't know about C).

There are two forms of parameter-declaration-clause,

parameter-declaration-list[OPT] ...[OPT]
parameter-declaration-list, ...

It seems you've focused only the second form.

You might ask, what would something like void foo(...){} be good for, since
there's no argument there to anchor the argument retrieval?

And one answer is that the ellipsis ... ranks lowest of all possible matches
for an actual argument, and for this usage it would just be silly to require
an extra argument in front.
 
B

Branimir Maksimovic

Hi,

I know that there is a difference (in C) between

int fun(void);

int fun();

The first one takes no arguments. The second can take any number of
arguments, including 0.

I'm wondering if this difference is still hold in C++.

No, and that is a very good thing.
When I was C beginner, that lack of prototyping in C was a real problem
for me (and I dind't know that one should always prototype functions
back in 1987.).

int main()
{
int i = malloc(32);
int *p = malloc(32);
free(i);
free(p);
return 0;
}

This C code compiles and links without problem, but causes lot of trouble.

Greetings, Bane.
 
R

Rolf Magnus

Alf said:
You might ask, what would something like void foo(...){} be good for,
since there's no argument there to anchor the argument retrieval?

My question would be how to retrieve the arguments, since va_start needs the
last fixed argument, which in this case doesn't exist.
 
B

Bob Hairgrove

You might ask, what would something like void foo(...){} be good for, since
there's no argument there to anchor the argument retrieval?

I've only seen this syntax used in catch statements (i.e. as a
"catch-all" last resort). It is sometimes dangerous, though --
depending on the platform, it could be a source of memory leaks if
used indiscriminately.
 
A

Alf P. Steinbach

* Rolf Magnus:
My question would be how to retrieve the arguments, since va_start needs the
last fixed argument, which in this case doesn't exist.

That's the same question; already answered. ;-)
 
B

Branimir Maksimovic

Jay Nabonne said:
No.

[nan@athena test]$ cat test2.c
void foo()
{
}
int main()
{
foo(1,2);
return 1;
}
[nan@athena test]$ g++ -Wall -x c test2.c
[nan@athena test]$ g++ -Wall -x c++ test2.c
test2.c: In function `int main()':
test2.c:2: error: too many arguments to function `void foo()'
test2.c:7: error: at this point in file

There's a difference between a function prototype ("declaration") and a
function definition.

Change the above "foo" to:

extern void foo();

and it will work fine. The assumption is that there is a real function
somewhere that has some sort of arguments. The danger is you're not
matching the real argument definition, so the compiler has to guess based
on the arguments you pass.

Of course, that's all C, so I may have a detail or two wrong...

function definition with empty parenthesis in c also means no arguments.
this is valid only for declaration, so you are right, but g++ would compile
it
as c++ anyway; one must use gcc for c compiling.

void foo();

int main()
{
foo(1);
foo(1,'c',3.2);
return 0;
}

void foo()
{
}

compiles with no problem in c.

Greetings, Bane.
 
J

Jay Nabonne

No.

[nan@athena test]$ cat test2.c
void foo()
{
}
int main()
{
foo(1,2);
return 1;
}
[nan@athena test]$ g++ -Wall -x c test2.c
[nan@athena test]$ g++ -Wall -x c++ test2.c
test2.c: In function `int main()':
test2.c:2: error: too many arguments to function `void foo()'
test2.c:7: error: at this point in file

There's a difference between a function prototype ("declaration") and a
function definition.

Change the above "foo" to:

extern void foo();

and it will work fine. The assumption is that there is a real function
somewhere that has some sort of arguments. The danger is you're not
matching the real argument definition, so the compiler has to guess based
on the arguments you pass.

Of course, that's all C, so I may have a detail or two wrong...

- Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top