问题描述
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; }};
智商跟不上,求解.
问题解答
回答1:在class内部定义的简单函数默认内联,不然就只有传函数指针。函数指针只能跳转。
回答2:我贴个英文原文,希望有帮助。
Exercise 16.31Explain how the compiler might inline the call to the deleter if we used DebugDelete with unique_ptr.