Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

c++ - `f(void)` meaning no parameters in C++11 or C?

In C++11 the following function declaration:

int f(void);

means the same as:

int f();

A parameter list consisting of a single unnamed parameter of non-dependent type void is equivalent to an empty parameter list.

I get the (perhaps false) impression this is an old feature, perhaps inherited from C?

Does anyone know the history or rationale behind this way to declare a function with no parameters?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In C++ they both mean the same thing.

In C f(void) is different from f(), becuse f() means "unspecified parameters" - you can legally pass anything (whether the function at receiving the data is happy about that or not is another matter).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...