c++ - How to use an overloaded function as an argument of function template? -


I think everyone has the experience of working with a code as follows:

  zero fun type1 & amp; nbsp;); Zero Funny (Type 2 & amp;;); Of vector & lt; Type 1 & gt; Vec; For_each (vec.begin (), vec.end (), fun); Of course it will not be compiled because it is not clear which function will pass. And what is your commonly used solution to the problem?  

I know this will work:

  for_each (vec.begin (), vec.end (), (zero (*) (type 1 & amp; )) Fun);  

But no better idea?

Using a solution template function:

  Lt; Typename T & gt; Zero Fun (T & amp;); // Funny for Type 1 and Type 2 ... for_each (vec.begin (), vec.end (), joy  

Better method is to use functor with template operator () :

  struct fun {template's  ; Typename t & gt; Zero operator () (T & amp; Const; }; ... for_each (vec.begin (), vec.end (), fun ()); // T operator () will automatically be deducted  

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -