免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2018 | 回复: 2

[C++] 用『可变模版参数』写个print [复制链接]

论坛徽章:
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
发表于 2013-07-19 12:15 |显示全部楼层
希望printf可以不需要%d,%f等来指示参数类型,因为一不小心经常会指错,而且某些类型(比如time_t,fpos_t等)还实现相关。
以下只是一个Demo,完美的最终形态应该类似于 printf( 12.3, {“%+06.1”,12.3}, 123, {HEX,123} );
以下代码在MinGW4.8.1上编译通过
  1. #include <cstdio>

  2. template<typename T> void print( const T& a );

  3. template<typename Head, typename... Last>
  4. void print( Head head, Last... last )
  5. {
  6.         print( head );
  7.         printf( ", " );
  8.         print( last... );
  9. }

  10. template<> void print<char>( const char& a )
  11. {
  12.         printf( "%c", a );
  13. }
  14. template<> void print<const char*>( const char* const& a )
  15. {
  16.         printf( "%s", a );
  17. }
  18. template<> void print<int>( const int& a )
  19. {
  20.         printf( "%d", a );
  21. }
  22. template<> void print<double>( const double& a )
  23. {
  24.         printf( "%f", a );
  25. }
  26. // [对以上代码的解释] 需要将所有内建类型都写上,这里只写了几个,意思一下而已

  27. struct foo
  28. {
  29.         unsigned val;
  30. };
  31. template<> void print<foo>( const foo& a )
  32. {
  33.         printf( "%c", 'A'+(a.val%25) );
  34. }
  35. // [对以上代码的解释] 这里可以定义 自定义类型 的输出,只是一个DEMO而已

  36. int main( void )
  37. {
  38.         print( 'a', "Hello world", 1, 23.456, foo{0} );

  39.         return 0;
  40. }
复制代码

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2013-07-20 01:34 |显示全部楼层
楼主对函数做特化是闲的无聊么?
  1.     #include <cstdio>

  2.     void print( const char& a )
  3.     {
  4.             printf( "%c", a );
  5.     }
  6.     void print( const char* const& a )
  7.     {
  8.             printf( "%s", a );
  9.     }
  10.     void print( const int& a )
  11.     {
  12.             printf( "%d", a );
  13.     }
  14.     void print( const double& a )
  15.     {
  16.             printf( "%f", a );
  17.     }
  18.     struct foo
  19.     {
  20.             unsigned val;
  21.     };
  22.     void print( const foo& a )
  23.     {
  24.             printf( "%c", 'A'+(a.val%25) );
  25.     }
  26.     // [对以上代码的解释] 这里可以定义 自定义类型 的输出,只是一个DEMO而已

  27.     template<typename Head, typename... Last>
  28.     void print( Head head, Last... last )
  29.     {
  30.             print( head );
  31.             printf( ", " );
  32.             print( last... );
  33.     }

  34.     int main( void )
  35.     {
  36.             print( 'a', "Hello world", 1, 23.456, foo{0} );

  37.             return 0;
  38.     }

复制代码

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2013-07-20 01:45 |显示全部楼层
本帖最后由 lost_templar 于 2013-07-20 01:46 编辑

而不用 iostream 简化代码:
  1. #include <iostream>

  2. struct foo
  3. {
  4.     unsigned val;
  5. };

  6. std::ostream& operator << ( std::ostream& os, foo const& a )
  7. {
  8.     os << static_cast<char>('A' + ( a.val % 25 ) );
  9.     return os;
  10. }

  11. template<typename T>
  12. void print( T const& val )
  13. {
  14.     std::cout << val;
  15. }

  16. template<typename Head, typename... Last>
  17. void print( Head head, Last... last )
  18. {
  19.     print( head );
  20.     printf( ", " );
  21.     print( last... );
  22. }

  23. int main( void )
  24. {
  25.     print( 'a', "Hello world", 1, 23.456, foo {0} );
  26.     return 0;
  27. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP