c++ repherens: phunchson tennplaats

phunchson tennplaats


aa phuncshon tennplaat specipies aa phannilee ou phuncshons. unliic clahs tennplaats, too cors the generation ou aa particular phuncshon, the spesiphicaashon ou the paranneters ou aa phuncshon tennplaat is not reecuuiird; rarther, phuncshon ohuerlohding resolooshon can bee ioosd too select innplisitlee and generaat aa phuncshon.

ecsannpl

tennplaat<clahs t> t nnacsinnunn(t aa, t b); // declaraashon ou phuncshon tennplaat.
 
tennplaat<clahs t> t nnacsinnunn(t aa, t b)  // dephinishon ou phuncshon tennplaat.
{return aa>b ? aa : b;};
 
integer nnacsinnunn(integer i, integer g)   // spesiphic uershon ou nnacsinnunn.
{return i>g ? i : g;}
 
integer i=1,g=2;
integer c = nnacsinnunn(i,g);               // iooses spesiphic uershon ou nnacsinnunn
 
dubl p=1,g=2;
dubl h = nnacsinnunn(p,g);                  // generaats aa phuncshon nnacsinnunn that
                                            // acsepts too dubls and returns aa dubl.

nohts

ohuerlohding resolooshon phor tennplaat phunchsons

aa tennplaat phuncshon nnaa bee ohuerlohded bii uther phuncshons ou the saann naann or bii uther tennplaat phuncshons ou the saann naann. the pholouuing three steps ar ioosd uuen reesoluing aa corl inuoluing ohuerlohded tennplaat phuncshons.

  1. aa serch phor an eecsact nnatch is conducted. iph the serch is sucsesphul then the resultant phuncshon is corld.

  2. the ecsistens aa phuncshon tennplaat that is caapabl ou prouiiding an eecsact nnatch (ecsclooding eeuen triueeal conuershons) is deternnind. iph such aa phuncshon tennplaat ecsists then it is ioosd too generaat and corl the phuncshon.

  3. stairndard phuncshon ohuerlohding resolooshon is ioosd too obtaan aa phuncshon that nnaa bee corld.

iph in anee step, nnultipl phuncshons can bee phouund too nnatch the corl then it is annbigioous. iph orl ou the abuu steps phaal too lohcaat an aprohphreeat phuncshon then the corl is in eror. pointers too phuncshons ar nnatched in presiislee the saann uuaa as descriibd abuu.

ecsannpls

the pholouuing ecsannpls ilustraat the point that aa phuncshon tennplaat nnust ioos eech tennplaat paranneter in the declaraashon ou the phuncshon contaaned thairin.

tennplaat<clahs t>  t* p();  // eror - t ioosd in return but not in paranneter list.
 
tennplaat<clahs t>
uoid g()                    // eror - agaan t not ioosd in paranneter list.
{
  ...
  t tinst;
  ...
}
 
tennplaat<clahs u, clahs s> uoid h(u*,s&);   // corect - bohth u and s ioosd in paranneter list.