C ++ selects function by return type

I understand that standard C ++ only selects functions by argument type, and not by return type. I can do something like:

void func(int); void func(double); 

but not

 double func(); int func(); 

Where in the former, it is clear, in the latter, it is ambiguous. Are there any extensions that let me tell C ++ about which function to use with the return type as well?

Thanks!

+4
source share
9 answers

You cannot have two functions in the same scope that have the same name and signature (i.e. argument types). However, you can create a function that will behave differently depending on which variable you assign, for example:

 int x=f(); double x=f(); // different behaviour from above 

making f() return the proxy with the overloaded casting operator.

 struct Proxy { operator double() const { return 1.1; } operator int() const { return 2; } }; Proxy f() { return Proxy(); } 

See http://ideone.com/ehUM1

Not that this particular use case (returning a different number) is useful, but they are used there for this idiom.

+10
source

If you have these two functions that the compiler needs to choose if you just called:

 func(); 

The closest thing you can do to what you ask is to use a specialized function template (note that you want to be very careful when specializing function templates ):

 template <typename ReturnT> ReturnT func(); template <> double func<>() { return 42; } template <> int func<>() { return 0; } 

Then you can call it like this:

 func<int>(); func<double>(); 
+9
source

Why not just name them differently? If they return different things, it sounds to me as if they are probably doing different things. Why obfuscate your code?

+5
source

In C ++, there is one context where the result of overload resolution depends on the type of return on the left side of the expression. This is the initialization / assignment of a function pointer value with a function address. It works with an explicit object of left size, as well as with a temporary object created by an explicit type.

In your case, it can be used to select one specific function from two overloaded ones. For instance:

 int (*pfunc)() = func; // selects `int func()` int i = pfunc(); // calls `int func()` 

You can use this technique to force overload resolution on a single line, although it does not look too elegant.

 int i = ((int (*)()) func)(); // selects and calls `int func()` 

Again, in this case, you perform manual overload resolution. C ++ does not have a function that will lead to implicit resolution of overload based on the return type (besides what I illustrated above).

+1
source

Since the compiler does not know what function you are calling!

0
source

Not; this is also quite difficult to implement. I vaguely remember that this is a P-NP problem.

0
source

No, of which I know. Your best bet is to name the functions differently or perhaps use a different argument to allow the compiler (and not you) to distinguish them.

Alternatively, follow these steps:

 struct myReturnType { int myInt; double myDouble; }; myReturnType func() { ... } 
0
source

This is a little dangerous, but you can create a union of the different types you want to return, and combine them into one function that returns the union. For instance:

 typedef union { double d; int i; } my_union; my_union func(); 

However, this will require a function and the caller to somehow find out what type of data to expect as a result, and can lead to all kinds of errors. You can do this, but I definitely do not recommend it.

0
source

function overloading requires different signatures of each form, excluding the return type. two or more forms are correct if and only if they in the list differ in the number of parameters or in at least one type or order of parameters, for example:

 void print(); // 1 void print(int, char); // 2 ok number of parameters void print(char, int); // 3 ok the order of parameters type int print(int, char); // 4 no the same signature as 2 except the return type 
0
source

Source: https://habr.com/ru/post/1308082/


All Articles