【问题标题】:Function template is not a plain function函数模板不是普通函数
【发布时间】:2014-07-29 17:52:20
【问题描述】:

为什么下面的代码给我们编译错误:

#include <iostream>
using namespace std;

int i = 0;

struct A
{
    void * operator new [] (size_t t)
    {
        cout << "allocation" << endl;
        return ::operator new[](t);
    }

    template <class T>
    void operator delete [] (void *p, size_t)
    {
        cout << "deallocation" << endl;
        return ::operator delete[](p);
    }
};

int main() 
{
    A *a = new A[10];
    delete [] a;//No suitable deallocation function
}

demo

我认为function template 只是一个匹配适当模板的家庭功能。这意味着必须在结构定义中找到非放置函数。

【问题讨论】:

    标签: c++ templates


    【解决方案1】:

    您的运算符delete[] 采用void*,但有一个模板参数T,它不用作函数签名中的参数类型,因此编译器无法解析函数调用。

    你想要:

    void operator delete [] (void *p, size_t);
    

    【讨论】:

    • 无法解析函数调用是什么意思?编译器如何解析函数调用?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    • 1970-01-01
    相关资源
    最近更新 更多