Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you make sure a C++ function can be called as [duplicate]

Tags:

c++

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)?

like image 294
Haider Kumail Avatar asked Feb 09 '21 18:02

Haider Kumail


People also ask

How to call a function from a program in C?

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.

How do you call a function without a parameter in C?

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.

What is the difference between function and called function in C?

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 ...

What does call by value mean in C programming?

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.


1 Answers

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.

like image 158
HolyBlackCat Avatar answered Oct 20 '22 12:10

HolyBlackCat