解释编译器把删除器处理为内联形式的可能方式?

cpp-primer5上ch16.31的问题,无法理解题目:
如果我们把一个自定义删除器DebugDelete与unique_ptr一起使用,解释编译器把删除器处理为内联形式的可能方式。
这个删除器只是简单的delete _ptr;而已。

#pragma once
#include <iostream>
class DebugDelete
{
    std::ostream &os;
public:
    DebugDelete(std::ostream &io = std::cerr):os(io) {}
    template<class T>
    void operator()(T *ptr)
    {
        os << "DebugDelete!!!";
        delete ptr;
    }
};

智商跟不上,求解.

阅读 2.9k
2 个回答

在class内部定义的简单函数默认内联,不然就只有传函数指针。函数指针只能跳转。

我贴个英文原文,希望有帮助。

Exercise 16.31
Explain how the compiler might inline the call to the deleter if we used DebugDelete with unique_ptr.

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题