How can you make sure a C++ function can be called as e.g. void foo(int, int) but not as any other type like void foo(long, long)?
There are two ways that a C function can be called from a program. They are, 1. Call by value: In call by value method, the value of the variable is passed to the function as parameter. The value of the actual parameter can not be modified by formal parameter. Different Memory is allocated for both actual and formal parameters.
In C, to declare a function that can only be called without any parameter, we should use “void fun (void)” As a side note, in C++, empty list means function can only be called without any parameter.
While creating a C function, you give a definition of what the function has to do. To use a function, you will have to call that function to perform the defined task. When a program calls a function, the program control is transferred to the called function. A called function performs a defined task and when its return statement is executed or ...
By default, C uses call by value to pass arguments. In general, it means the code within a function cannot alter the arguments used to call the function.
Add a deleted template overload:
template <typename A, typename B> void foo(A, B) = delete;
void foo(int x, int y) {...}
It will be a better match (causing a error) for any argument types except int, int
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With