如何解决 typename 的命名冲突?

anrxhzh 2002-07-29 12:09:12
struct C {
typedef int it;
};

template <typename T>
struct test {
typedef T::it it;
};

//struct T {}; //如果去掉注释则会造成混乱

int main()
{
test<C>::it i=0;
}
...全文
380 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
prototype 2002-07-29
  • 打赏
  • 举报
回复
typedef typename t::it it;

> //struct T {}; //如果去掉注释则会造成混乱
don't know what you exactly mean here. :( what do you refer to by '混乱'?
zheng_can 2002-07-29
  • 打赏
  • 举报
回复
最最简单的解决方法就是不要这么命名
这么短的类名是不被提倡的
anrxhzh 2002-07-29
  • 打赏
  • 举报
回复
使用名字空间也不好解决。

我现在明白为什么在C++库的编写规范中,所有的参数类型名字都为_[A-Z][^_].*,所有的用户类型名字都为[a-z].*了。
tomPeakz 2002-07-29
  • 打赏
  • 举报
回复
我对付这种命名冲突的办法 - namespace
namespace AAA{
typedef int it;

}
namespace BBB{
template <typename T>
struct test {
typedef typename T::it it;
};
}
using namespace AAA;
using namespace BBB;
//or AAA::it BBB::it
划分名字空间
你可以试试。
anrxhzh 2002-07-29
  • 打赏
  • 举报
回复
谢谢 prototype(原型)。

不好意思,我没有表达清楚。实际上我又掉进编译器的陷阱了,如果去掉注释,Microsoft Visual C++ 6.0 就会报错,这是由参数类型T 和全局类型T同名而引起的。我现在已经被这种陷阱搞地麻木了:-)

严格的讲,应该这样写:

struct C {
typedef int it;
};

template <typename T>
struct test {
typedef typename T::it it;
};

struct T {};

int main()
{
test<C>::it i=0;
}

在 Dev-C++ 4.9.4.1 下编译通过。
在 VC 6.0 下通不过。

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧