C++11 lambda and template specialization

后端 未结 2 742
忘了有多久
忘了有多久 2021-02-06 17:05

I would like to know what is the correct type definition for the lambda presented below, so that the following code will compile using a conformant c++11 compiler:



        
2条回答
  •  -上瘾入骨i
    2021-02-06 17:45

    It's auto + decltype:

    auto l = [](int i) -> bool { printf("%d",i); return true; };
    foo fi(l);
    fi.fum();
    

    Every single lambda has a different, unique, unnamed type. You, as a coder, just can not name it.

    However, in your case, since the lambda doesn't capture anything (empty []), it is implicitly convertible to a pointer-to-function, so this would do:

    foo fi([](int i) -> bool { printf("%d",i); return true; });
    fi.fum();
    

提交回复
热议问题