c++ - deducting a type T for std::function from a lambda function -


i trying understand how following code works :

#include <iostream> #include <functional>  using namespace std;  template <typename t> struct identity {     typedef t type; };  template <typename t> void foo(t val, typename identity<function<void(t)>>::type f) {     f(val); }  int main () {     foo(1337,[](int x){ cout << "lambda " << x << endl; });      return 0; } 

there no choice of t make lambda type equal std::function(void(t))type, that's why metafunction needed.

i know nested-name-specifier :: brick wall make compiler not try deduce type t parameter.

so tried same code without t val function parameters , t not deduced.

does mean compiler following steps when call

foo(1337,[](int x){ cout << "lambda " << x << endl; });

  1. arg 1337 , param t val // t deducted int
  2. brick wall, won't try deduce type second argument

will compiler call identity<function<void(int)>> using type t deducted step 1 or else?

yes, believe you're right.

unfortunately, language template argument deduction in standard incredibly verbose, daren't begin trying prove fact.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

javascript - Passport with Express 4 -