在 G++ 4.8 中,typeof 仍然不能与 "::" 一起使用
In G++ 4.8, typeof still cannot be used with "::"
以下代码不在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;
但仍然很烦人。
这是一个应该报告的错误吗?或者这是一个功能?
这里我只是写下n.m.的评论作为答案,并对其进行了一点扩展。
在C++11中,我们有decltype
,它可以与::
一起使用。考虑以下代码:
#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
,您的代码将是标准的,因此是可移植的。
相关文章:
- 为什么我不能将 rand() 与数组的大小一起使用?
- 不能将复制初始化与隐式转换的多个步骤一起使用
- Python 3.8 不能与 Pybind11 一起导入 [Windows]
- Turbo C++ cin() 不能与 gets() 一起使用
- 不能将重载比较运算符与 Catch 测试一起使用
- 不能将C++的"std::filesystem"库与介子构建一起使用
- C++程序在将 int 与 cin 一起使用时有效,但不能使用字符串
- std::remove() 按预期处理文字,但不能与取消引用的迭代器一起工作
- 为什么我不能将运算符+与reverse_iterator一起使用?
- 为什么引用和指针不能一起使用?
- C++:printf 和 wprintf 不能一起工作 (Linux)
- Apple Mach-O Linker (ld) 错误.ld: -r 和 -dead-strip 不能一起使用
- typeinfo name() 和 endl 在 Windows 和 mingw 中不能一起工作
- boost和Curl不能一起工作
- Q_ENUMS和using声明不能一起工作
- 为什么linux管道和标准输出不能一起工作
- c++ /OpenGL: vao单独工作,但不能一起工作
- 类型定义和函数声明不能一起工作
- pcap_stats和pcap_dump_open不能一起工作
- OSX 10.8.5 上的 Android 构建错误:cp:-R 和 -r 选项不能一起指定