模板减去数组大小 [英] template deduction of array size

查看:37
本文介绍了模板减去数组大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有谁知道这是否应该有效?


模板< unsigned N>

int strn( const char str [N])

{

返回N;

}


#include < iostream>


int main()

{

std :: cout<< strn(abcd);

}


错误:没有匹配函数来调用`strn(const char [5])''


Does anyone know if this is supposed to work ?

template <unsigned N>
int strn( const char str[N] )
{
return N;
}

#include <iostream>

int main()
{
std::cout << strn( "abcd" );
}

error: no matching function for call to `strn(const char[5])''

推荐答案

> template< unsigned N>
> template <unsigned N>
int strn(const char str [N])
{
返回N;
}

#包括< iostream>



std :: cout<< strn(abcd);
}
错误:没有匹配函数来调用`strn(const char [5])''
int strn( const char str[N] )
{
return N;
}

#include <iostream>

int main()
{
std::cout << strn( "abcd" );
}

error: no matching function for call to `strn(const char[5])''




我想不是因为Comeau和VC ++ 7.1都不接受它(结果如下),但

我必须承认我为什么感到困惑。我知道一般来说,函数模板的非类型

参数可以推导出来,但显然有一些关于这个案例的特殊内容。我将有兴趣关注这个

线程...


请注意,如果您将参数更改为对数组的引用,它可以工作.. 。

也许这可以暗示你提出的案件失败的原因......

Comeau:

" ComeauTest.c",第11行:错误:没有函数模板的实例" strn"

匹配

参数列表

您使用的参数类型是:(const char [5])

std :: cout<< strn(" abcd");


VC ++ 7.1:

d:\ visual_c ++ \dot_net\parameter_test\parameter_tes t\main.cpp (11):错误

C2784:''int strn(const char [N])'':无法推断

''const char [N]的模板参数''from''const char [5]''

d:\ visual_c ++ \dot_net\parameter_test\parameter_tes t\main.cpp(4):

见strn的声明



I think not since neither Comeau nor VC++ 7.1 accept it (results below), but
I must admit I am perplexed as to why. I know that in general a nontype
parameter for a function template can be deduced, but there is clearly
something special about this case. I''ll be interested to follow this
thread...

Note that if you change the parameter to bre reference to array, it works...
Maybe this can hint at why the case you presented fails...
Comeau:
"ComeauTest.c", line 11: error: no instance of function template "strn"
matches the
argument list
The argument types that you used are: (const char [5])
std::cout << strn("abcd");

VC++ 7.1:
d:\visual_c++\dot_net\parameter_test\parameter_tes t\main.cpp(11) : error
C2784: ''int strn(const char [N])'' : could not deduce template argument for
''const char [N]'' from ''const char [5]''
d:\visual_c++\dot_net\parameter_test\parameter_tes t\main.cpp(4) :
see declaration of ''strn''


2003年12月3日美国东部时间17:27:52,Gianni Mariani< gi ******* @ mariani。 ws>

写道:
On 03 Dec 2003 17:27:52 EST, Gianni Mariani <gi*******@mariani.ws>
wrote:

有谁知道这是否应该有效?

模板< unsigned N>
int strn(const char str [N])
{
返回N;
}

#include< iostream>

int main()
{
std :: cout<< strn(abcd);
}
错误:没有匹配函数来调用`strn(const char [5])''

Does anyone know if this is supposed to work ?

template <unsigned N>
int strn( const char str[N] )
{
return N;
}

#include <iostream>

int main()
{
std::cout << strn( "abcd" );
}

error: no matching function for call to `strn(const char[5])''




Geez,我很久以前读过CUJ的一篇文章,处理了这个非常好的b $ b情况...我认为你需要用sizeof()进行逐神化。


希望我能在正确的方向上激励你。



Geez, I read an article in CUJ long time ago that dealt with this very
situation... I think you need to templetize with sizeof().

Hope I inspire you in the right general direction.




" Gianni Mariani" < GI ******* @ mariani.ws>在消息中写道

news:bq ******** @ dispatch.concentric.net ...

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bq********@dispatch.concentric.net...

有谁知道这是否是假设工作?

模板< unsigned N>
int strn(const char str [N])
{
返回N;
}

#include< iostream>

int main()
{
std :: cout<< strn(abcd);
}
错误:没有匹配函数来调用`strn(const char [5])''

Does anyone know if this is supposed to work ?

template <unsigned N>
int strn( const char str[N] )
{
return N;
}

#include <iostream>

int main()
{
std::cout << strn( "abcd" );
}

error: no matching function for call to `strn(const char[5])''




另一个想法:参数和参数都应该衰减为

指针。 (如果它是参考,那么论证就不会,但是这不是

的情况......)。我想知道这是否会以某种方式干扰扣除

流程......



One other thought: Both the parameter and the argument should decay to a
pointer. (The argument would not if it were reference, but that''s not the
case here...). I wonder if this is somehow interfering with the deduction
process...


这篇关于模板减去数组大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆