Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template deduction of array size

Reply
Thread Tools

template deduction of array size

 
 
Gianni Mariani
Guest
Posts: n/a
 
      12-03-2003

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])'


 
Reply With Quote
 
 
 
 
Dan W.
Guest
Posts: n/a
 
      12-03-2003
On 03 Dec 2003 17:27:52 EST, Gianni Mariani <>
wrote:

>
>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, 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.

 
Reply With Quote
 
 
 
 
Dave
Guest
Posts: n/a
 
      12-03-2003
> 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])'


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'


 
Reply With Quote
 
Dave
Guest
Posts: n/a
 
      12-03-2003

"Gianni Mariani" <> wrote in message
news:bqlo18$...
>
> 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...


 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      12-03-2003
Dave wrote:

>>Does anyone know if this is supposed to work ?
>>
>>template <unsigned N>
>>int strn( const char str[N] )
>>{
>> return N;
>>}


....

>>
>>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...


Yes, a reference seems to work, but why does the non reference version
fail ?

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

#include <iostream>

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


OK - I solved my problem.

 
Reply With Quote
 
Dan W.
Guest
Posts: n/a
 
      12-03-2003
On Wed, 03 Dec 2003 17:42:53 -0500, Dan W. <>
wrote:

>On 03 Dec 2003 17:27:52 EST, Gianni Mariani <>
>wrote:
>
>>
>>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, 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.


What's wrong with sizeof() itself?
 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      12-03-2003
Gianni Mariani wrote in news:bqlo18$:

>
> Does anyone know if this is supposed to work ?
>
> template <unsigned N>
> int strn( const char str[N] )
> {
> return N;
> }


This is effectivly:

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

>
> #include <iostream>
>
> int main()
> {
> std::cout << strn( "abcd" );
> }
>
> error: no matching function for call to `strn(const char[5])'


Yup the call needs to be:

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

Where 5 can be any compile time integral constant you want.

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

Or get creative:

template < typename T, unsigned N >
unsigned countof( T const (&array)[ N ] )
{
return N;
}

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
Dan W.
Guest
Posts: n/a
 
      12-03-2003
On 03 Dec 2003 18:11:37 EST, Gianni Mariani <>
wrote:

>Dave wrote:
>
>>>Does anyone know if this is supposed to work ?
>>>
>>>template <unsigned N>
>>>int strn( const char str[N] )
>>>{
>>> return N;
>>>}

>
>...
>
>>>
>>>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...

>
>Yes, a reference seems to work, but why does the non reference version
>fail ?
>
>template <unsigned N>
>int strn( const char ( & str )[N] )
>{
> return N;
>}
>
>#include <iostream>
>
>int main()
>{
> std::cout << strn( "abcd" );
>}
>
>
>OK - I solved my problem.


I think that in the case of the array, the number in the brackets is
never passed to the function at runtime; --i.e.: it is equivalent to
passing a pointer, and the number in the brackets is just candy for
your eyes only; whereas a reference is a unique type.

 
Reply With Quote
 
Dan W.
Guest
Posts: n/a
 
      12-03-2003
>
>I think that in the case of the array, the number in the brackets is
>never passed to the function at runtime; --i.e.: it is equivalent to
>passing a pointer, and the number in the brackets is just candy for
>your eyes only; whereas a reference is a unique type.


Of course! int[5] is not a compile-time type, but a reference to
int[5] IS a type.

Just a C clingon.

 
Reply With Quote
 
Dave
Guest
Posts: n/a
 
      12-04-2003
> > 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])'

>
> 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'
>
>


Has a conclusion been reached on *why* the original code (shown at the top)
fails to compile? Even though we have determined that using a reference
fixes the problem, it is still unclear to me why the original program fails
to compile...


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
template class instantiate without template parameter, automatic type deduction Fei Liu C++ 4 10-26-2007 02:39 PM
template class instantiate without template parameter, automatictype deduction Fei Liu C++ 0 10-25-2007 08:12 PM
Template argument deduction Peng Yu C++ 1 04-16-2005 04:30 PM
Template argument deduction and conversion operators. BigMan@abv.bg C++ 3 02-02-2005 09:15 PM
Is This Legal Template Code? (Deduction Rules(?)) Chris Johnson C++ 3 08-14-2003 01:15 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57