Chinaunix首页 | 论坛 | 博客
  • 博客访问: 831463
  • 博文数量: 158
  • 博客积分: 4380
  • 博客等级: 上校
  • 技术积分: 2367
  • 用 户 组: 普通用户
  • 注册时间: 2006-09-21 10:45
文章分类

全部博文(158)

文章存档

2012年(158)

我的朋友

分类: C/C++

2012-11-26 16:00:11

#include

                                                      // VC9   g++4.5

template void foo1( int buf[N][N+1] )    {} // 不能  不能

template void foo2( int buf[][N+1] )     {} // 不能  不能

template void foo3( int buf[N-1][N] )    {} // 不能      // 奇怪的VCfoo5能推导出,为什么foo3不行?

template void foo4( int (&buf)[N-1][N] ) {} //      

template void foo5( int buf[][N] )       {} //      

template void foo6( int (&buf)[N][N+1] ) {} // 不能      // 奇怪的VCfoo4能推导出,为什么foo6不行?

 

int main()

{

     int buf[1][2];

 

     foo1( buf );

     foo2( buf );

     foo3( buf );

     foo4( buf );

     foo5( buf );

     foo6( buf );

}

////////////////////////////////////////////////////////////////////////////////

另外,如下代码在VC2008中竟然不能编译通过,且错误提示实在太奇怪了,已经指定了模板参数,它为什么要自己推导呢?

struct foo { template<typename U> static int bar(); enum { SIZE = sizeof( foo::bar<int>() ) // VC2008报:could not deduce template argument for 'U' }; };





to 无聊者:
要想编译通过,只需要将foo::bar()改为bar()就行了,但这和问题无关,不是我要的。我想知道的是:为什么正确的语法,但不能编译通过。
例如,我将 struct foo 也变成一个模板,且其模板参数不是int,则 foo::bar() 中 foo 就不是可以省略的内容。



阅读(1152) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~