c++ - Function passed as template argument -


I am acting as the argument of the C ++ template function.

It is supported by the example C ++:

  #include & lt; Iostream & gt; Zero add1 (int and v) {v + = 1; } Zero add2 (int & v) {v + = 2; } Template & lt; Void (* T) (int & amp;) & gt; Zero doOperation () {int temp = 0; T (temporary); Std :: cout & lt; & Lt; "The result is" & lt; & Lt; Temporary & lt; & Lt; Std :: endl; } Int main () {doOperation & lt; Add1 & gt; (); DoOperation & LT; Add2 & gt; (); }  

Learning about this technique is difficult, yet does not take much, and the classic does not even talk surprisingly (at least not from my search).

I have questions that this is valid C ++ (or just some widely supported extensions).

Also, is there a way to allow a Makkar to use each other with the same signature with explicit actions during this type of template charge?

In the following no works, the above program, at least, because the syntax is clearly incorrect. If you want to define custom comparison operation, then stad :: It is a good idea to be able to switch the function for a factor and vice versa as the function pointer or factor to sort function of the sort algorithm. / P>

  straight add3 {zero operator () (nt and v) {v + = 3;}}; ... doOperation & lt; Add3 & gt; ();  

Indicators for a page in a web link or two, or C ++ template book will be appreciated!

Yes, it is valid.

Also work with the Fanter to create this, the general solution is something like this:

  template & lt; Typename F & gt; Zero doOperation (f) {int temp = 0; F (temporary); Std :: cout & lt; & Lt; "The result is" & lt; & Lt; Temporary & lt; & Lt; Std :: endl; }  

which can now be said either:

  doOperation (add2); DoOperation (add3 ());  

The problem with this is that if it makes the caller compile call to add2 , because all the compilers know that the function pointer Type is being done near zero (*) (int & amp;) to doOperation (but add3 , being a fun, Here, the compiler knows that a type of object add3 function has been passed, which means that the funk Action call add3 :: operator () , and not just some unknown function pointer.)


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -