Newbie query: Cline & Lomow, C++ FAQs (No. 55)...in C++ f(); is == to f(void);

H

hugo2

obrhy[at]yahoo.com July 7, 2005

Suppose a user function is declared, prototyped f();,
where the () are empty.
And further, when the function is defined in a .CPP file
it has no params, f(){...}.

But when this function is called, invoked in the program
its call has an argument: f(*thresh1);

Would this be an error?
Or does the function have a way of accessing the arg(s)?

hugo-----
 
P

Peter Julian

hugo2 said:
obrhy[at]yahoo.com July 7, 2005

Suppose a user function is declared, prototyped f();,
where the () are empty.
And further, when the function is defined in a .CPP file
it has no params, f(){...}.

But when this function is called, invoked in the program
its call has an argument: f(*thresh1);

Correction: f(*thresh1) isn't a call to f() since the supplied parameter(s),
or lack thereof, are part of a function's signature.

The same condition exists if you define void f() and attempt to call it
with:
f(arg); // attempts to call void f(T t) where T is the arg's Type
If void f(T t) isn't defined, and no auto-conversion exists between supplied
and expected parameters, error ensues.

Beware if void f(int n) is defined and then:
double d;
f(d);
In the above statements, a silent cast from double to int is carried out (a
rather unfortunate situation in most cases). That silent cast is about as
loose as a C++ compliant compiler is allowed to get with respect to a
function call.
Would this be an error?
Or does the function have a way of accessing the arg(s)?

Its an error, but what needs to be noted is that the function definition
results in a signature. That signature is composed of the function name +
arguments and its usually padded by the compiler (the return type is *not*
part of the signature).

So instead of thinking of this call as a function call, think of it as a
reasonably strict function-signature calling mechanism (for lack of a better
phrase).

There is no way the compiler can deduce, inject or guess at what it should
do if unexpected parameters are provided (its not allowed to act
ambiguously). If C++ allowed this, overloading functions would not be
posssible. That is, void f(int n), void f(char c), void f(), etc all depict
unique signatures.
 
U

upashu2

Beware if void f(int n) is defined and then:But compiler will give warning : double to int : possible loss of Data
(VC++)
 
V

Victor Bazarov

upashu2 said:
But compiler will give warning : double to int : possible loss of Data
(VC++)

If you want to give compiler-specific answers, please go to that compiler
newsgroup. Compilers are not required to give any _warnings_ about loss
of data. Only diagnostics required are about ill-formed programs and then
not all cases of ill-formedness _require_ a diagnostic.

V
 
P

Peter Julian

To avoid silent casting have all variables be the same type if possible

Thats a ridiculous comment. To avoid silent casting one needs to overload
appropriately. Thats withput mentioning templates.

#include <iostream>

void f(int d)
{
std::cout << "f(int) called\n";
std::cout << "d = " << d << "\n";
}

void f(double d)
{
std::cout << "f(double) called\n";
std::cout << "d = " << d << "\n";
}

template< class T >
void templated(T& t)
{
std::cout << "templated(T&) called\n";
std::cout << "t = " << t << "\n";
}

int main()
{
double num(5.1);

f(num);
templated(num);

return 0;
}

/*
f(double) called
d = 5.1
templated(T&) called
t = 5.1
*/
 
P

Peter Julian

upashu2 said:
But compiler will give warning : double to int : possible loss of Data
(VC++)

Irrelevent, a programmer doesn't rely on some particular compiler feature.
See the overloaded + templated code posted above in response to zab1000's
comments.
 
H

hugo2

Peter said:
Correction: f(*thresh1) isn't a call to f() since the supplied parameter(s),
or lack thereof, are part of a function's signature.

The same condition exists if you define void f() and attempt to call it
with:
f(arg); // attempts to call void f(T t) where T is the arg's Type
If void f(T t) isn't defined, and no auto-conversion exists between supplied
and expected parameters, error ensues.

Beware if void f(int n) is defined and then:
double d;
f(d);
In the above statements, a silent cast from double to int is carried out (a
rather unfortunate situation in most cases). That silent cast is about as
loose as a C++ compliant compiler is allowed to get with respect to a
function call.


Its an error, but what needs to be noted is that the function definition
results in a signature. That signature is composed of the function name +
arguments and its usually padded by the compiler (the return type is *not*
part of the signature).

Thanks Peter Julian.
Well, in the executable program, if a function is called it's a call
by anyother name to me...

I quest it would be an error. From my efforts in Javascript, which
one may call a junior OOP, if a function is defined without parameters
but is called with some arg list, then the engine automaticaly
populates a house array named arguments and the recieving function
may read the args via the array idexes: arguments[0]...arguments[n]
and the number of args with arguments.length.

C++ would have to have a similar arangement, and I saw no sign of any.
Thanks again, PJ. hugo------
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top