Home > Back-end >  How to choose parameters can accept lambda, at the same time the function, the function pointer
How to choose parameters can accept lambda, at the same time the function, the function pointer

Time:11-30

Why can't the lambda automatically pushed to function

CodePudding user response:

can use a template or function as a parameter

Using func=STD: : functionUsing functptr=void (*) (void);

Template
Void f1 (t& & Func) {
Func ();
}
Template
Void f1 (t& & First, u& & . Another) {
F1 (first);
F1 (another... );
}

Void f2 (func f) {
(f);
}

Int main () {
Auto test1=[] () {STD: : cout & lt; <"Lambda"; };
Func test2=[] () {STD: : cout & lt; <"Function"; };
Functptr test3=[] () {STD: : cout & lt; <"The function pointer"; };
/*
STD: : cout & lt; */
F1 (test1 and test2 and test3);
//print the lambda function fuction pointer
F2 (test1); F2 (test2); F2 (test3);
//print the lambda function fuction pointer
}

CodePudding user response:

Pointer to void * is universal

CodePudding user response:

Use Pointers can receive directly,

CodePudding user response:

 
The template & lt; The class T>
Void foreach (const STD: : vector & StrVec, T callable)
{
For (size_t I=0; i If (callable (strVec [I]) {//... } else {//... }
}
}

CodePudding user response:

Can you? Who said no
  • Related