c++ - 不能使用从 std::unary_function 继承的 typedef

标签 c++

<分区>

prog.cpp:9:13: error: ‘result_type’ does not name a type
prog.cpp:9:13: note: (perhaps ‘typename std::unary_function<_Arg, _Result>::result_type’ was intended)

编译器: http://ideone.com/vttG8W

为什么我不能直接使用 result_type?

#include <functional>

using namespace std;

template <typename ARGUEMENT, typename RESULT>
class FunctorBase : public std::unary_function<ARGUEMENT, RESULT>
{
public:
    virtual result_type operator () (argument_type) = 0;
        FunctorBase() {}
        virtual ~FunctorBase() {}
};

int main()
{
    FunctorBase<int&, void>();
}

最佳答案

因为它是一个不合格的名字

当你在类模板中使用非限定名称时,你必须告诉编译器它不应该在两阶段名称查找的第一阶段立即搜索全局名称,而是等到实例化(因为名称可能来自基类,就像这里的情况一样)。

在这种情况下,您可以这样做(并使 result_type 成为 dependent,限定名称):

typedef typename std::unary_function<ARGUEMENT, RESULT>::result_type result_type;

请注意,这同样适用于 argument_type

typedef typename std::unary_function<ARGUEMENT, RESULT>::argument_type argument_type;

有了这两个 typedef,原来的成员函数声明现在可以编译了:

virtual result_type operator () (argument_type) = 0;

关于c++ - 不能使用从 std::unary_function 继承的 typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15552825/

相关文章:

c++ - 具有相同索引的for循环的性能

c++ - Glibc 的静态链接

c++ - cmake 在 opencv c++ 项目中不工作

从指针到指针的 C++ 新指针?

html - 获取html代码时出错

c++ - 嵌套结构字段的 Clang 格式规则

C++ UNIX 错误 : Undefined first referenced symbol in file

c++ - 如何为 native 单元测试添加额外的 dll 搜索目录?

c++ - C/C++判断文件是否写完

c++ - 如何检查类型是否存在无参数运算符()