请教:解释一下这段模板特化的问题

sakurar 2003-10-10 03:09:25
#include <stdio.h>

template<typename T>
struct box
{
template<typename TK>
struct test
{
void ShowType(void)
{
printf( "\n俺是未定义" );
}
};

template<>
struct test<int>
{
void ShowType(void);
};
};

template<typename T>
template<TK>
void box<T>::test<int>::ShowType(void)
{
printf("\n俺是int");
}

void main(void)
{
box<int>::test<int> s;
s.ShowType();

getchar();
}


//上面这一小段代码可以正常的编译,但无法通过链接。为什么呢?
//如果把对int的特化的定义放在box的定义里一起定义的话,就一切正常了。
//感觉是特化的地方写的不对,请帮助改正。谢谢。

...全文
48 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
sakurar 2003-10-12
  • 打赏
  • 举报
回复
To: xueweizhong(薛卫忠)

加typename我也试过,但不知为什么不能通过链接。
Wolf0403 2003-10-12
  • 打赏
  • 举报
回复
呵,Dev-C++ 的 MinGW 根本不支持非 namespace scope 的模板偏特化
xueweizhong 2003-10-12
  • 打赏
  • 举报
回复
是这一段代码出现了问题:
template <typename T> template <>
box<T>::test<int>::ShowType(void)
{
printf( "俺是int\n" );
}

你找其他编译器试一下,就会知道具体错误了。
我想自从herb sutter和 lippman到了MS之后
模板的支持的确到了非常好的地步。
但他们似乎也漏掉了某些东西。
Wolf0403 2003-10-12
  • 打赏
  • 举报
回复
很奇怪,我的 2003 出现的是两个错误。然后修改成这样之后:
#include <stdio.h>

template<typename T>
struct box
{
template<typename TK>
struct test
{
void ShowType(void);
};

template<>
struct test<int>
{
void ShowType(void);
};
};

template <typename T> template <typename TK>
box<T>::test<TK>::ShowType(void)
{
printf( "俺是未定义\n" );
}

template <typename T> template <>
box<T>::test<int>::ShowType(void)
{
printf( "俺是int\n" );
}

void main(void)
{
box<int>::test<int> s;
s.ShowType();
}
错误:
e:\Visual Studio Projects\CppTemplate\CppTemplate\CppTemplate.cpp(26): fatal error C1001: 内部编译器错误
(编译器文件“msc1.cpp”的第 2701 行)
。请选择 Visual C++
“帮助”菜单上的“技术支持”命令,或打开技术支持帮助文件获得详细信息
xueweizhong 2003-10-11
  • 打赏
  • 举报
回复
我特意去试了.net2003,有点让我觉得以外的是:
楼主的代码修改如下的地方后,可以编译通过:

template<typename T>
template<TK>
void box<T>::test<int>::ShowType(void)
{
printf("\n俺是int");
}

------>

template<typename T>
template<typename TK>
^^^^^^^^
void box<T>::test<int>::ShowType(void)
{
printf("\n俺是int");
}

然后就出现了楼主所说的链接错误的问题。

对我来说.net2003允许
“在模板类作用域内显示特殊化”
虽然不符合c++98,但还是个好消息。

但模板类的成员模板类的成员函数定义
如何在名字空间作用域内定义的问题,
容我再考虑一下。
xueweizhong 2003-10-10
  • 打赏
  • 举报
回复
虽然我没用过.net2003,
但是我想你贴的代码即使用.net2003编译,
也不能通过。
能把编译过的代码贴一下吗?
xueweizhong 2003-10-10
  • 打赏
  • 举报
回复
template<typename T>
struct box
{
...
template<>
^^^^^^^^^^
struct test<int>
{
void ShowType(void);
};
};

在模板体内不能进行explicit specialization
不知道你是在哪个编译器下编译的?
sakurar 2003-10-10
  • 打赏
  • 举报
回复
我用的是VisualStudio.NET2003。

export关键字不支持
Wolf0403 2003-10-10
  • 打赏
  • 举报
回复
template<TK>
这个是什么?
xueweizhong 2003-10-10
  • 打赏
  • 举报
回复
不好意思, todd0 is me
todd0 2003-10-10
  • 打赏
  • 举报
回复
这里是一个替代方案:

enum e_single { single };
^^^^^^^^^^^^^^^^^^^
template <class>
struct out
{
template <class T, e_single = single>
^^^^^^^^^^^^^^^^^^
struct in {};
};


template <class T> template <e_single unused>
^^^^^^^^^^^^^^^
struct out<T>::in<int, unused>
^^^^^^
{
};

这里我故意让e_single的值是unique的,但同时也达到了
出现另外一个模板参数而骗过编译器的限制。
todd0 2003-10-10
  • 打赏
  • 举报
回复
to xueweizhong(薛卫忠)
帮你up一下, 请继续。
xueweizhong 2003-10-10
  • 打赏
  • 举报
回复
下面这部分代码
template <typename S>
template <>
struct Out<S>::In<int>
{
void foo()
{
}
};

出现如下错误:
"ComeauTest.c", line 14: error: a template declaration containing a template
parameter list may not be followed by an explicit specialization
declaration
template <>
^
template explicit specilization 和
template partial specilization必须
出现在namespace scope, (by C++98).
所以楼主的代码不能放在类内部。

但是放在类外部后,
由于
template <typename> template<>
出现错误。

所以只能改为:
template <>
template <>
struct Out<char>::In<int>
{... };

template <>
template <>
struct Out<float>::In<int>
{ ... };

template <>
template <>
struct Out<int* >::In<int>
{ ... };

应该有其他的解决方案,
但似乎没有了最原始的代码那样直接、简单、明显。
xueweizhong 2003-10-10
  • 打赏
  • 举报
回复
template <typename S>
struct Out
{
template <typename T>
struct In {};
};

template <typename S>
template <typename T>
struct Out<S>::In<T*>
{};

template <typename S>
template <>
struct Out<S>::In<int>
{
void foo()
{
}
};

int main()
{
Out<char>::In<int> i;
i.foo();
}

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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