Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't a return type be function type but function pointer in c++?

Tags:

c++

int method1() {
    return 0;
}

decltype(method1) method2() {
    return method1;
}

I compile my code,get an error: ‘method2’ declared as function returning a function, then I change the return type to function pointer,it works,I just want to why it is so.

decltype(method1) *method2() {
    return method1;
}
like image 710
cong Avatar asked Jul 28 '26 14:07

cong


1 Answers

In C++, functions and arrays cannot be returned. It is how the language is designed.

You have to return either pointer or reference to them. You've already tried with pointer, which works. The following, which returns reference, should also work:

decltype(method1)& method2() {
   return method1;  
}
like image 158
Nawaz Avatar answered Jul 30 '26 03:07

Nawaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!