Lambda expressions as class template parameters

前端 未结 5 1574
星月不相逢
星月不相逢 2020-11-29 20:05

Can lambda expressions be used as class template parameters? (Note this is a very different question than this one, which asks if a lambda expression itself can be

5条回答
  •  忘掉有多难
    2020-11-29 20:28

    @Xeo gave you the reason, so I'll give you the work around.

    Often times you do not wish to name a closure, in this case, you can use std::function, which is a type:

    typedef std::unordered_map<
      std::string,
      std::string,
      std::hash,
      std::function
      > map_type;
    

    Note that it captures exactly the signature of the function, and no more.

    Then you may simply write the lambda when building the map.

    Note that with unordered_map, if you change the equality comparison, you'd better change the hash to match the behavior. Objects that compare equal shall have the same hash.

提交回复
热议问题