C++使用别名访问嵌套类型(使用 vs typedef)
C++ accessing nested type using alias (using vs typedef)
今天我们发现了一个令人困惑的C++11别名声明行为。下面是示例:
template<typename T>
struct Q
{
typedef T t;
};
template<typename T>
void foo(Q<T> q)
{
using q_t = Q<T>;
//typedef Q<T> q_t; // if we uncomment this and comment 'using' the example compiles
typename q_t::t qwe; // <<<<<<<<<< Error: no type named ‘t’ in ‘using q_t = struct Q<T>’
}
int main(int argc, char *argv[])
{
Q<int> q;
foo(q);
return 0;
}
ISO 14882(C++11)规定这两个声明必须具有相同的语义(第145页)。
但是,如果我们q_t声明了"using",则该示例不会使用 GCC 4.7.2(Debian Wheezy)和 GCC 4.7.3(Ubuntu 13.04)进行编译,但将"using"语句替换为"typedef"语句使其编译。
是GCC中的错误还是我们只是误解了标准?
这似乎是GCC 4.7的错误。
这是我编译代码的测试,它使用 gcc 4.8.1 工作
因此,正如规范所说:
using q_t = Q<T>;
// is equivalent to this
typedef Q<T> q_t;
使用
--std=c++11 使用 g++ 4.8.1 为我编译得很好
相关文章:
- 使用VS Code和CMake Tools运行自定义命令
- C++新手,想知道如何使用VS code 2019播放音频文件
- 可视化 使用 VS Code 查找C++应用程序中的内存泄漏
- 如何使用VS 2015构建Boost 1.44
- 我正在使用 VS 代码,但 cout 不起作用
- 在使用 vs 代码调试文件时,如何将文件传递到我的程序中
- 使用 VS 2015 的编译器警告 4456
- 使用VS Community 2017打开VDPROJ文件
- 由于Memcpy的问题,无法使用VS进行优化;被视为固有功能
- 使用VS公共继承模板化
- 如何与CMAKE一起使用VS 2017编译的Boost Filesystem库
- Eclipse CDT-输出 - 使用VS编译器时无用
- Visual Studio 2013 在使用 VS 2010 平台工具集时找不到头文件
- 在使用 VS-add 创建项目时不能包含 Qt 类
- 使用 vs typedef 指针到noexcept-function不一致
- SDL 2.0使用VS生成错误
- 当我们在使用VS的C++项目中引用库作为附加依赖项时,会发生什么
- 使用 VS 宏设置预处理器定义
- Cstring 在 VS 2012 中不起作用,但如果使用 VS 2012 打开 VC++ 6.0 项目,则可以工作
- MASM 使用 VS 击败未优化的 .cpp 但不是未优化的 .c