Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply

C++ - template deduction of array size

 
Thread Tools Search this Thread
Old 12-03-2003, 09:27 PM   #1
Gianni Mariani
 
Posts: n/a
Default template deduction of array size


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
Old 12-03-2003, 09:42 PM   #2
Dan W.
 
Posts: n/a
Default Re: template deduction of array size

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
Old 12-03-2003, 09:54 PM   #3
Dave
 
Posts: n/a
Default Re: template deduction of array size

> 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
Old 12-03-2003, 10:00 PM   #4
Dave
 
Posts: n/a
Default Re: template deduction of array size


"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
Old 12-03-2003, 10:11 PM   #5
Gianni Mariani
 
Posts: n/a
Default Re: template deduction of array size

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
Old 12-03-2003, 10:20 PM   #6
Dan W.
 
Posts: n/a
Default Re: template deduction of array size

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
Old 12-03-2003, 10:25 PM   #7
Rob Williscroft
 
Posts: n/a
Default Re: template deduction of array size

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
Old 12-03-2003, 10:27 PM   #8
Dan W.
 
Posts: n/a
Default Re: template deduction of array size

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
Old 12-03-2003, 10:34 PM   #9
Dan W.
 
Posts: n/a
Default Re: template deduction of array size

>
>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
Old 12-04-2003, 03:06 AM   #10
Dave
 
Posts: n/a
Default Re: template deduction of array size

> > 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 Search this Thread
Search this Thread:

Advanced Search

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Panasonic Widscreen 86cm TV Info Needed Screen Size TX-86PW300A. The Other Guy. DVD Video 1 06-23-2010 02:20 AM
ARRAY(n DOWNTO 0) OF STD_LOGIC_VECTOR(m DOWNTO 0) - VHDL freitass Hardware 0 11-01-2007 02:44 PM
Looking for 2-up CD label software or template M.L. DVD Video 14 05-31-2007 12:49 AM
How to split a large avi file into smaller avi by size Avner DVD Video 1 10-31-2005 05:34 PM
Question: Windows Explorer File Sizes JS A+ Certification 4 10-27-2003 12:46 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

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