How to avoid explicit cast with std::bind() temporary objects?

孤街醉人 提交于 2019-12-08 03:50:28

Best to give Callback a universal constructor:

struct Callback
{
    typedef std::function<void(int)> ftype;
    ftype fn_;

    template <typename T,
              typename = typename std::enable_if<std::is_constructible<ftype, T>::value>::type>
    Callback(T && f) : fn_(std::forward<T>(f))
    { }
};

(I added the second, defaulted template argument to only enable this constructor for types T for which the statement makes sense, so as not to create false convertibility properties.) Note how this technique re­moves one implicit user-defined conversion from the conversion chain, by invoking an explicit con­struc­tor for fn_.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!