免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5568 | 回复: 9
打印 上一主题 下一主题

[C++] 类成员函数模板特化 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-08-19 16:01 |只看该作者 |倒序浏览
本帖最后由 Redshadows 于 2013-08-19 16:33 编辑

test.h

  1. #pragma once
  2. namespace TT
  3. {

  4. struct No
  5. {
  6.         int i;       
  7. };

  8. struct Test;

  9. void nothing(Test &test);

  10. struct Test
  11. {
  12.         friend void nothing(Test &test);
  13.         template <typename T> void show(const T& tt);
  14. //        template <> void show(const int& tt);
  15.         void show1();
  16. };
  17. }
复制代码
test.cpp
  1. namespace TT;

  2. template<typename T>
  3. void Test::show(const T& tt)
  4. {
  5.         cout << "test" << endl;
  6. }

  7. template<>
  8. void TT::Test::show(const TT::No& tt)
  9. {
  10.         cout << "haha" << endl;
  11. }

  12. void Test::show1()
  13. {
  14.         show(1);
  15. }
复制代码
main.cpp
  1. #include "test.h"

  2. int main()
  3. {
  4.         TT::Test test;
  5.         int a = 0;
  6.         test.show1();
  7.         nothing(test);
  8.         return 0;
  9. }
复制代码
编译后提示
test.cpp:14: error: specialization of ‘template<class T> void TT::Test::show(const T&)’ in different namespace
test.h:17: error:   from definition of ‘template<class T> void TT::Test::show(const T&)’
test.cpp:16: confused by earlier errors, bailing out
Preprocessed source stored into /tmp/ccDOpYyR.out file, please attach this to your bugreport.
Traceback (most recent call last):
  File "/usr/share/apport/gcc_ice_hook", line 34, in <module>
    pr.write(open(apport.fileutils.make_report_path(pr), 'w'))
IOError: [Errno 13] Permission denied: '/var/crash/_usr_lib_gcc_i486-linux-gnu_4.3.3_cc1plus.1000.crash'

求指点。

论坛徽章:
4
双子座
日期:2014-08-28 10:08:002015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:58:112015年亚洲杯之阿联酋
日期:2015-03-13 03:25:15
2 [报告]
发表于 2013-08-19 16:46 |只看该作者
template<>
void TT::Test::show(const TT::No& tt)
{
        cout << "haha" << endl;
}

这个函数是什么意思?  删掉试试

论坛徽章:
0
3 [报告]
发表于 2013-08-19 16:58 |只看该作者
weishuo1999 发表于 2013-08-19 16:46
template
void TT::Test::show(const TT::No& tt)
{

似乎是C++不允许类成员函数模板特化。

论坛徽章:
17
处女座
日期:2013-08-27 09:59:352015亚冠之柏太阳神
日期:2015-07-30 10:16:402015亚冠之萨济拖拉机
日期:2015-07-29 18:58:182015年亚洲杯之巴勒斯坦
日期:2015-03-06 17:38:17摩羯座
日期:2014-12-11 21:31:34戌狗
日期:2014-07-20 20:57:32子鼠
日期:2014-05-15 16:25:21亥猪
日期:2014-02-11 17:32:05丑牛
日期:2014-01-20 15:45:51丑牛
日期:2013-10-22 11:12:56双子座
日期:2013-10-18 16:28:17白羊座
日期:2013-10-18 10:50:45
4 [报告]
发表于 2013-08-19 17:25 |只看该作者
回复 1# Redshadows


    这是VC的代码在gcc(g++)下是编不过的。你的程序有几个问题:
1:g++不支持成员函数的模板特化。
2:namespace TT;是不是少写了个using
3:#pragma once在gcc中是被列淘汰的特性,不应该使用。

论坛徽章:
17
处女座
日期:2013-08-27 09:59:352015亚冠之柏太阳神
日期:2015-07-30 10:16:402015亚冠之萨济拖拉机
日期:2015-07-29 18:58:182015年亚洲杯之巴勒斯坦
日期:2015-03-06 17:38:17摩羯座
日期:2014-12-11 21:31:34戌狗
日期:2014-07-20 20:57:32子鼠
日期:2014-05-15 16:25:21亥猪
日期:2014-02-11 17:32:05丑牛
日期:2014-01-20 15:45:51丑牛
日期:2013-10-22 11:12:56双子座
日期:2013-10-18 16:28:17白羊座
日期:2013-10-18 10:50:45
5 [报告]
发表于 2013-08-19 17:29 |只看该作者
回复 3# Redshadows

C++标准是支持类模板成员函数特化的。但是gcc(g++)不支持。


C++03, §14.7.3/2:
An explicit specialization shall be declared in the namespace of which the template is a member, or, for member templates, in the namespace of which the enclosing class or enclosing class template is a member.An explicit specialization of a member function, member class or static data member of a class template shall be declared in the namespace of which the class template is a member.
   

论坛徽章:
0
6 [报告]
发表于 2013-08-20 09:38 |只看该作者
myworkstation 发表于 2013-08-19 17:25
回复 1# Redshadows

少了using应该是从linux下复制出来少了。

论坛徽章:
0
7 [报告]
发表于 2013-08-20 09:41 |只看该作者
myworkstation 发表于 2013-08-19 17:29
回复 3# Redshadows

C++标准是支持类模板成员函数特化的。但是gcc(g++)不支持。

OK thanks

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
8 [报告]
发表于 2013-08-20 09:51 |只看该作者
我将你的代码中根本没用到的代码删除,然后修补一下,在MinGW4.8.1和VC++9.0上都编译测试通过
  1. #include <iostream>
  2. using namespace std;

  3. namespace TT
  4. {
  5.     struct No
  6.     {
  7.         int i;
  8.     };

  9.     struct Test
  10.     {
  11.         template <typename T> void show(const T& tt);
  12.         void show1();
  13.     };

  14. }

  15. namespace TT
  16. {
  17.     template<typename T>
  18.     void Test::show(const T& tt)
  19.     {
  20.         cout << "test" << endl;
  21.     }

  22.     template<>
  23.     void TT::Test::show(const TT::No& tt)
  24.     {
  25.         cout << "haha" << endl;
  26.     }

  27.     void Test::show1()
  28.     {
  29.         show(1);
  30.         show( No() );
  31.     }

  32. }

  33. int main()
  34. {
  35.     TT::Test test;
  36.     test.show1();

  37.     return 0;
  38. }
复制代码

论坛徽章:
0
9 [报告]
发表于 2013-08-21 10:41 |只看该作者
x 模板能有cpp文件实现?

论坛徽章:
17
处女座
日期:2013-08-27 09:59:352015亚冠之柏太阳神
日期:2015-07-30 10:16:402015亚冠之萨济拖拉机
日期:2015-07-29 18:58:182015年亚洲杯之巴勒斯坦
日期:2015-03-06 17:38:17摩羯座
日期:2014-12-11 21:31:34戌狗
日期:2014-07-20 20:57:32子鼠
日期:2014-05-15 16:25:21亥猪
日期:2014-02-11 17:32:05丑牛
日期:2014-01-20 15:45:51丑牛
日期:2013-10-22 11:12:56双子座
日期:2013-10-18 16:28:17白羊座
日期:2013-10-18 10:50:45
10 [报告]
发表于 2013-08-21 11:07 |只看该作者
回复 9# superfeeler

在这个例子中是可以的。因为在实现模板的cpp文件中调用了模板函数,show(1);这个调把模板实例化了。所以不会出问题,如果把show1在头文件中实现(在头文件中定义函数show1并调用show(1))那么就会出现错误。由于模板解析和实例化的过程复杂所以一般推荐模板都实现在头文件中,不然会引出一堆的问题。
   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP