search

Home  >  Q&A  >  body text

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

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;
    }
};

智商跟不上,求解.

阿神阿神2804 days ago359

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 13:35:32

    Simple functions defined inside a class are inlined by default, otherwise only function pointers are passed. Function pointers can only jump.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:35:32

    I’ll post the original English text, hope it helps.

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

    reply
    0
  • Cancelreply