Method of substituting an anonymous function that takes a return value for a C++ function pointer variable

Asked 1 years ago, Updated 1 years ago, 399 views

Question details

Regarding the presentation code, I would like to substitute an anonymous function for the pointer variable related to the code below, but every implementation will fail.
How do I define an anonymous function when I take a return value?

What do you want to know

I want to know how to substitute an anonymous function to return to the function pointer variable

Tried and wanted to know

I tried many things like the //Implementation part of the presentation code, but all of them failed and I don't know which one is the closest correct answer.

Reference Site

Quiita: https://qiita.com/YukiMiyatake/items/8d10bca26246f4f7a9c8
Blog site A: https://marycore.jp/prog/cpp/pass-function-as-argument/
Blog site B: https://kaworu.jpn.org/cpp/%E3%83%A9%E3%83%A0%E3%83%80%E5%BC%8F#.E6.9C.80.E5.B0.8F.E3.81.AE.E3.83.A9.E3.83.A0.E3.83.80.E5.BC.8F.E3.81.AE.E5.AE.9A.E7.BE.A9

Source Code

#include<iostream>


int call() 
{
    return1;
}



// int(*f)() = [ ]( ) - > int {std::cout<<"sssss"<<std::endl;return0;}(); // Implementation 1
// int(*f)() = [ ]( ) - > int {std::cout<<"sssss"<<std::endl;return0;}(int); // Implementation 2
// int(*f)() = [ ]( ) - > int {std::cout<<"sssss"<<std::endl;}(0); // Implementation 3
int(*f)() = [ ]( ) - > int {std::cout<<"sssss"<<std::endl;} (return 0); // Implementation 4



// int(*f)() = call;


int main()
{       
    // std::cout<<call(fn)<<std::endl;
    
    return 0;
}

Error

$make
g++-c-MMD-MP src/Main.cpp-obj/Main.o-I./../src-I~/Library-I~/Library/freetype 
src/Main.cpp:21:68:error:invalid conversion from 'int' to 'int(*)()'[-fpermissive]
   21 | int(*f)() = [ ]( ) - > int {std::cout<<"sssss"<<std::endl;return0;}();
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
      |                                                                    |
      |                                                                    int
make:*** [Makefile:15:obj/Main.o] Error 1
$make
g++-c-MMD-MP src/Main.cpp-obj/Main.o-I./../src-I~/Library-I~/Library/freetype 
src/Main.cpp:22:69:error:expected primary-expression before 'int'
   22 | int(*f)() = [ ]( ) - > int {std::cout<<"sssss"<<std::endl;return0;}(int); // Implementation 2
      |                                                                     ^~~
make:*** [Makefile:15:obj/Main.o] Error 1

Implementation 3 Error

$make
g++-c-MMD-MP src/Main.cpp-obj/Main.o-I./../src-I~/Library-I~/Library/freetype 
src/Main.cpp:Inlambda function:
src/Main.cpp: 23:57: warning: no return statement in function returning non-void [-Wreturn-type]
   23 | int(*f)() = [ ]( ) - > int {std::cout<<"sssss"<<std::endl;}(0); // Implementation 3
      |                                                         ^
src/Main.cpp:At global scope:
src/Main.cpp:23:60:error:no match for call to '(<lambda()>)(int)'
   23 | int(*f)() = [ ]( ) - > int {std::cout<<"sssss"<<std::endl;}(0); // Implementation 3
      |                                                            ^
src/Main.cpp:23:60:note:candidate:'int(*)()'<conversion>
src/Main.cpp:23:60:note:candidate expectations 1 argument, 2 provisioned
src/Main.cpp:23:13:note:candidate:'<lambda()>'
   23 | int(*f)() = [ ]( ) - > int {std::cout<<"sssss"<<std::endl;}(0); // Implementation 3
      |             ^
src/Main.cpp:23:13:note:candidate expectations 0 arguments, 1 provisioned
make:*** [Makefile:15:obj/Main.o] Error 1

Implementation 4 Error

make
g++-c-MMD-MP src/Main.cpp-obj/Main.o-I./../src-I~/Library-I~/Library/freetype 
src/Main.cpp:Inlambda function:
src/Main.cpp: 24:57: warning: no return statement in function returning non-void [-Wreturn-type]
   24 | int(*f)() = [ ]( ) - > int {std::cout<<"sssss"<<std::endl;} (return 0); // Implementation 4
      |                                                         ^
src/Main.cpp:At global scope:
src/Main.cpp:24:59:error:expected primary-expression before 'return'
   24 | int(*f)() = [ ]( ) - > int {std::cout<<"sssss"<<std::endl;} (return 0); // Implementation 4
      |                                                           ^~~~~~
make:*** [Makefile:15:obj/Main.o] Error 1

c++

2023-01-02 08:44

1 Answers

The syntax of the lambda expression is as follows if you omit attributes and other specifications.

Capture List - >Return Value Type {Function Body}

After that, place the argument to invoke the lambda expression.

If you are substituting a function pointer, you do not need .


2023-01-02 08:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.