15

代码:

// test3.cpp

#include <stack>

using namespace std;

template<typename T>
struct ptr_stack_tp;

template<typename T>
struct ptr_stack_tp<T*> : public stack<T*>
{
    ~ptr_stack_tp()
    {
        while (!empty()) {
            operator delete(top());
            pop();
        }
    }
};

int main()
{}

错误消息(gcc 4.7.2):

test3.cpp: In destructor 'ptr_stack_tp<T*>::~ptr_stack_tp()':
test3.cpp:15:23: error: there are no arguments to 'empty' that depend on a template parameter, so a declaration of 'empty' must be available [-fpermissive]
test3.cpp:15:23: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
test3.cpp:16:33: error: there are no arguments to 'top' that depend on a template parameter, so a declaration of 'top' must be available [-fpermissive]
test3.cpp:17:17: error: there are no arguments to 'pop' that depend on a template parameter, so a declaration of 'pop' must be available [-fpermissive]

Functionsempty()和是top()pop()函数std::stack,那么,为什么 gcc 找不到它们?

4

1 回答 1

26

this您应该通过指针显式调用类模板中的基类成员函数。

// ...

template<typename T>
struct ptr_stack_tp<T*> : public stack<T*>
{
    ~ptr_stack_tp()
    {
        while (!this->empty()) {
        //      ^^^^^^
            operator delete(this->top());
            //              ^^^^^^
            this->pop();
        //  ^^^^^^
        }
    }
};

// ...

这是由于模板的两阶段名称查找工作方式。如果没有this->间接,编译器会尝试将非限定名称解析为全局函数的名称。由于不存在名为empty()top()pop()的全局函数,编译器将发出错误。

当您使用this->时,编译器会将名称查找延迟到模板实际实例化的那一刻:此时,对基类成员的函数调用将被正确解析。

于 2013-03-19T16:25:50.480 回答