免费注册 查看新帖 |

Chinaunix

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

[C++] 一个最基础的template疑问 [复制链接]

论坛徽章:
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
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-10-17 09:55 |只看该作者 |倒序浏览
10可用积分
本帖最后由 bruceteen 于 2013-10-17 10:26 编辑
  1. template<typename T>
  2. void foo( T& val ) {
  3. }

  4. int main()
  5. {
  6.     const int v = 0;
  7.     foo( v );

  8.     foo<const int>( 0 );

  9.     foo( 0 ); // 这个编译失败,为什么
  10.    
  11.     return 0;
  12. }
复制代码
请问,为什么 foo( 0 ) 不是实例为 foo<const int>,而是实例为 foo<int> ?

最佳答案

查看完整内容

回复 5# bruceteen 数学字面量(integer literal)的类型为能包含其值的整型类型,不能隐式进行CV转换。关于integer literal的描述细则如下: The type of an integer literal depends on its form, value, and suffix. If it is decimal and has no suffix, it hasthe first of these types in which its value can be represented: int, long int; if the value cannot be representedas a long int, the behavior is undefi ...

论坛徽章:
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
2 [报告]
发表于 2013-10-17 09:55 |只看该作者
回复 5# bruceteen


    数学字面量(integer literal)的类型为能包含其值的整型类型,不能隐式进行CV转换。关于integer literal的描述细则如下:
   The type of an integer literal depends on its form, value, and suffix. If it is decimal and has no suffix, it has
the first of these types in which its value can be represented: int, long int; if the value cannot be represented
as a long int, the behavior is undefined. If it is octal or hexadecimal and has no suffix, it has the
first of these types in which its value can be represented: int, unsigned int, long int, unsigned
long int. If it is suffixed by u or U, its type is the first of these types in which its value can be represented:
unsigned int, unsigned long int. If it is suffixed by l or L, its type is the first of these
types in which its value can be represented: long int, unsigned long int. If it is suffixed by ul,
lu, uL, Lu, Ul, lU, UL, or LU, its type is unsigned long int.


根据上面的规定可知0类型为int,与const int不匹配,又由于0是个临时对象其为rvalue,所以无法进行匹配转换。

论坛徽章:
4
白羊座
日期:2013-09-17 21:59:30技术图书徽章
日期:2013-10-12 22:16:03白羊座
日期:2013-10-14 11:01:40双子座
日期:2013-12-17 18:26:39
3 [报告]
发表于 2013-10-17 10:08 |只看该作者
回复 1# bruceteen
非const引用不能指向临时变量和const变量

   

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
4 [报告]
发表于 2013-10-17 10:13 |只看该作者
改成 void foo( T val )

论坛徽章:
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-10-17 10:52 |只看该作者
T& val 表示参数需要是的lvalue,而foo( 0 );表示的参数0是个rvalue。rvalue无法转换到lvalue,foo<const int>( 0 );相当于把形参实例化为const int& val,rvalue如果以引用方法访问只能以const-reference方式。

论坛徽章:
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
6 [报告]
发表于 2013-10-17 12:10 |只看该作者
myworkstation 发表于 2013-10-17 10:52
T& val 表示参数需要是的lvalue,而foo( 0 );表示的参数0是个rvalue。rvalue无法转换到lvalue,foo<const int>( 0 );相当于把形参实例化为const int& val,rvalue如果以引用方法访问只能以const-reference方式。

对于 foo( 0 ),编译器为什么不将那个模板实例化为
void foo( const int& val ) {
}
呢?
而 const int v = 0; foo( v ) 却可以。

论坛徽章:
59
2015年亚洲杯之约旦
日期:2015-01-27 21:27:392015年亚洲杯之日本
日期:2015-02-06 22:09:41拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015元宵节徽章
日期:2015-03-06 15:50:392015年亚洲杯之阿联酋
日期:2015-03-19 17:39:302015年亚洲杯之中国
日期:2015-03-23 18:52:23巳蛇
日期:2014-12-14 22:44:03双子座
日期:2014-12-10 21:39:16处女座
日期:2014-12-02 08:03:17天蝎座
日期:2014-07-21 19:08:47
7 [报告]
发表于 2013-10-17 12:40 |只看该作者
回复 5# bruceteen


    要问你自已为什么不将
  1. 01.template<typename T>

  2. 02.void foo( T& val ) {

  3. 03.}
复制代码
写成
  1. 01.template<typename T>

  2. 02.void foo(const T& val ) {

  3. 03.}
复制代码

论坛徽章:
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-10-17 13:48 |只看该作者
回复 6# folklore
别人的代码,虽然我知道怎么改可以正确编译,但却说不出为什么不改就编译失败。

论坛徽章:
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
9 [报告]
发表于 2013-10-17 14:17 |只看该作者
回复 8# myworkstation
先把分给你,但为什么 foo( (const int)0 ) 也编译失败?
  1. #include <iostream>
  2. #include <typeinfo>
  3. using namespace std;

  4. template<typename T>
  5. void foo( T& val ) {
  6. }

  7. int main()
  8. {
  9.     const int v = 0;
  10.     foo( v );

  11.     foo<const int>( 0 );

  12.     //foo( 0 );
  13.     //foo( (const int)0 ); 这个也失败

  14.     cout << typeid(v).name() << endl; // 输出 int / i
  15.     cout << typeid(0).name() << endl; // 输出 int / i
  16.     cout << typeid((const int)0).name() << endl; // 输出 int / i
  17.    
  18.     return 0;
  19. }
复制代码

论坛徽章:
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-10-17 14:25 |只看该作者
回复 9# bruceteen


    这个也失败是因为“rvalue无法转换到lvalue”, (const int)0依然是个rvalue
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP