1

以下代码在 G++ 4.8 下无法编译

#include <vector>
using namespace std;

int main() {
    vector<int> v;
    typeof(v)::iterator it;
}

如果我将 typeof 替换为 decltype,它可以正常工作。我知道使用模板结构的解决方法

template<class T> struct Self {
    typedef T Type;
};

接着

Self<typeof(v)>::Type::Iterator it;

但仍然很烦人。

这是一个应该报告的错误吗?或者这是一个功能?

4

1 回答 1

4

在这里,我只是写了nm 的评论作为答案并对其进行了一些扩展。

在 C++11 中,我们有decltypewhich 可以与::. 考虑以下代码:

#include <vector>
using namespace std;

int main() {
    vector<int> v;
    decltype(v)::iterator it;
}

上面的代码用g++ -std=c++11 -Wall -Wextra -pedantic typeof.cpp.

由于decltype它是标准的并且已经被 gcc 4.3(2008 年发布,6 年前)支持,因此绝对没有理由使用 gcc 扩展typeof。如果您使用decltype.

于 2014-04-18T08:58:14.127 回答