C++ std::unique_ptr : Why isn't there any size fees with lambdas?

后端 未结 4 2025
暖寄归人
暖寄归人 2020-12-28 12:03

I am reading \"Effective Modern C++\". In the item related to std::unique_ptr it\'s stated that if the custom deleter is a stateless object, then no size fees o

4条回答
  •  孤城傲影
    2020-12-28 12:52

    If the deleter is stateless there's no space required to store it. If the deleter is not stateless then the state needs to be stored in the unique_ptr itself.
    std::function and function pointers have information that is only available at runtime and so that must be stored in the object alongside the pointer the object itself. This in turn requires allocating (in the unique_ptr itself) space to store that extra state.

    Perhaps understanding the Empty Base Optimization will help you understand how this could be implemented in practice.
    The std::is_empty type trait is another possibility of how this could be implemented.

    How exactly library writers implement this is obviously up to them and what the standard allows.

提交回复
热议问题