Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not allowed to return a function from a function. How could I?

8.3.5/8 Functions [dcl.fct] says

[...] Functions shall not have a return type of type array or function, although they may have a return type of type pointer or reference to such things. [...]

Why so explicit of a rule? Is there some syntax that would even allow returning a function as opposed to a function pointer?

Am I miss-interpreting the quote?

typedef void (*fp)();

void foo(){}
fp goo()
{
    return foo; //automatically converted to function pointer
}
like image 564
Luchian Grigore Avatar asked Feb 13 '13 23:02

Luchian Grigore


People also ask

How do you make a function not return?

Void functions are created and used just like value-returning functions except they do not return a value after the function executes. In lieu of a data type, void functions use the keyword "void." A void function performs a task, and then control returns back to the caller--but, it does not return a value.

Can we return a function from a function in C++?

C++ Return Function Pointer From Function: To return a function pointer from a function, the return type of function should be a pointer to another function. But the compiler doesn't accept such a return type for a function, so we need to define a type that represents that particular function pointer.

What happens if a function doesn't have a return?

If no return statement appears in a function definition, control automatically returns to the calling function after the last statement of the called function is executed. In this case, the return value of the called function is undefined.

What a function Cannot return in C?

A function that returns void means it does not return a value at all. C does not allow you to use the return statement with an expression (even if it is a void one) if your function is declared as void .


2 Answers

This is quite a contrived example of a function trying to return a function:

void foo() { }

template<typename T>
T f() { return foo; }

int main(){
    f<decltype(foo)>();
}

This is the error I get from Clang 3.2:

Compilation finished with errors:
source.cpp:7:5: error: no matching function for call to 'f'
    f<decltype(foo)>();
    ^~~~~~~~~~~~~~~~
source.cpp:4:3: note: candidate template ignored: substitution failure 
[with T = void ()]: function cannot return function type 'void ()'
T f() { return foo; }
~ ^
1 error generated.
like image 101
Andy Prowl Avatar answered Sep 19 '22 11:09

Andy Prowl


Is there some syntax that would even allow returning a function as opposed to a function pointer?

A syntax? Sure there is:

using fun = int (int);

fun function_that_returns_a_function();

That doesn’t compile because the rule in §8.3.5/8 forbids it. I don’t know why the rule specifically exists – but consider that the type “function” doesn’t have any size so you cannot create objects of function type in C++.

like image 34
Konrad Rudolph Avatar answered Sep 18 '22 11:09

Konrad Rudolph