![]() |
|
|
|||||||
![]() |
C++ - template deduction of array size |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
Posts: n/a
|
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])' |
|
|
|
#2 |
|
Posts: n/a
|
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. |
|
|
|
#3 |
|
Posts: n/a
|
> 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' |
|
|
|
#4 |
|
Posts: n/a
|
"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... |
|
|
|
#5 |
|
Posts: n/a
|
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. |
|
|
|
#6 |
|
Posts: n/a
|
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? |
|
|
|
#7 |
|
Posts: n/a
|
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/ |
|
|
|
#8 |
|
Posts: n/a
|
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. |
|
|
|
#9 |
|
Posts: n/a
|
>
>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. |
|
|
|
#10 |
|
Posts: n/a
|
> > 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... |
|
![]() |
| Thread Tools | Search this Thread |
|
|
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 |