如何制作这个在模板构造函数复制中使用类型定义的类型的模板
How to make this template which using typedefed type in template constructor copmiles?
#include <iostream>
template <typename T>
struct W
{
typedef T TT;
W(const typename W<int>::TT & m)
{
std::cout << "here" << std::endl;
}
};
int main()
{
int f;
W<int>k( f );
return 0;
}
VC11 可以编译,但 G++ 不起作用。
我的 gcc 版本:
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=d:/usr/app/mingw-w32/bin/../libexec/gcc/i686-w64-mingw32/4.7
.0/lto-wrapper.exe
Target: i686-w64-mingw32
Configured with: ../../../build/gcc/src/configure --target=i686-w64-mingw32 --pr
efix=/c/bb/vista64-mingw32/mingw-x86-x86/build/build/root --with-sysroot=/c/bb/v
ista64-mingw32/mingw-x86-x86/build/build/root --enable-languages=all,obj-c++ --e
nable-fully-dynamic-string --disable-multilib
Thread model: win32
gcc version 4.7.0 20111219 (experimental) (GCC)
GCC 错误消息: dd.cc:7:27: error: 'TT' in 'struct W<int>' does not name a type
你不能那样做。
为了知道typename W<int>::TT
是什么类型,编译器必须实例化W<int>
,但它不能在已经实例化W<int>
的情况下执行此操作,因为类型在声明构造函数时不完整。
完整类型与其构造函数之间存在循环依赖关系。
Clang给出了一个有用的错误:
t.cc:7:22: error: implicit instantiation of template 'W<int>' within its own definition
W(const typename W<int>::TT & m)
^
不确定它是如何在 VC++ 中编译的
但是以下编译与 MinGW g++ 4.7.2
#include <iostream>
template <typename T>
struct W
{
typedef T TT;
W(const TT & m) // remove typename W<int> or use W(const W::TT & m)
{
std::cout << "here" << std::endl;
}
};
int main()
{
int f;
W<int> k( f );
return 0;
}
- 有没有一种方法可以通过"typedef"为重新定义的基本类型定义特征和强制转换运算符
- 强枚举类型定义:Clang Bug 还是 C++11 标准不确定性?
- 列表参数的类型定义
- 使用模板化的键类型定义 std::map,该键类型基于作为参数接收的函数
- 关于 C++ 中的函数类型定义
- C++(和 ROS) - 包含与前向声明引用,设置默认值和类型定义
- 将使用/类型定义限制为类范围
- 模板类型定义?
- C++:模板类的类型定义
- 如何对命名空间限定类型进行类型定义?
- 此递归模板类型定义是否有效C++?
- 具有调整对齐方式的类型定义
- C++从抽象类型定义类成员
- 用于C++代码的 API 监视器类型定义 (XML)
- 如何将result_of与函数类型定义一起使用
- 在C++的适当类型定义位置
- 如何根据模板类型定义浮点常量?
- 如何为缺少预定义运算符而不扩展命名空间"std"的标准类型定义运算符>> (istream &, ...)?
- 参数化类的别名(或类型定义)内部类
- 如果我想从类型"T"定义元素的容器(来自 STL),那么"T"必须使用默认构造函数?