473,545 Members | 2,469 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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])'
Jul 22 '05 #1
14 3890
> 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++\d ot_net\paramete r_test\paramete r_test\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++\d ot_net\paramete r_test\paramete r_test\main.cpp (4) :
see declaration of 'strn'
Jul 22 '05 #2
On 03 Dec 2003 17:27:52 EST, Gianni Mariani <gi*******@mari ani.ws>
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.

Jul 22 '05 #3

"Gianni Mariani" <gi*******@mari ani.ws> wrote in message
news:bq******** @dispatch.conce ntric.net...

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...
Jul 22 '05 #4
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.

Jul 22 '05 #5
Gianni Mariani wrote in news:bq******** @dispatch.conce ntric.net:

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/
Jul 22 '05 #6
On Wed, 03 Dec 2003 17:42:53 -0500, Dan W. <da**@raytron-controls.com>
wrote:
On 03 Dec 2003 17:27:52 EST, Gianni Mariani <gi*******@mari ani.ws>
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?
Jul 22 '05 #7
On 03 Dec 2003 18:11:37 EST, Gianni Mariani <gi*******@mari ani.ws>
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.

Jul 22 '05 #8
>
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.

Jul 22 '05 #9
> > 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++\d ot_net\paramete r_test\paramete r_test\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++\d ot_net\paramete r_test\paramete r_test\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...
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
7859
by: Raja | last post by:
How to know the buffer size and increase buffer size in c++.
5
6440
by: amparikh | last post by:
Hi all, I was just browsing the archives when I saw one fo the messages posted recently about array size template et al. All along, I have been using the usual template to get the size of the array. template<typename T, std::size_t N> std::size_t size_of(T(&)) { return N;
13
21345
by: HappyHippy | last post by:
Hi, I'm wondering what you think about this piece of code: #include<iostream> int main() { int size; std::cin >> size;
8
17527
by: Jeff | last post by:
Hello everybody, I'm kind of new to C programming, but here's a little question. Usually, when you have an array of chars, you put a \0 at the end of it to terminate the string. That way, it is possible with functions like strlen to get the array size. But is it possible to do the same thing with an array of integers or floats? And are...
4
2805
by: terry | last post by:
Hi, Could anyone tell me how to determine the size of array of characters dynamically? For example, : : char *a={"hello","hi","kitty"}; char *b={"orange","apple"};
7
5625
by: arkobose | last post by:
hey everyone! i have this little problem. consider the following declaration: char *array = {"wilson", "string of any size", "etc", "input"}; this is a common data structure used to store strings of any lengths into an array of pointers to char type variable. my problem is: given the declaration
4
21695
by: Bilgehan.Balban | last post by:
Hi, The following code: #include <stdio.h> // const int const_asize = 10; #define define_asize = 10; int array = {1,2,3,4,5,6,7,8,9,0};
2
2396
by: Harry | last post by:
Good Day To all, When i am declaring a array for e.g char ....it means i am declaring array of 45 characters each of which has a maximum,minimum value limit or range...for example in VC++(compiler that i am using,it has nothing to do with C++) char's range is -128 to +127....this is the range for each char element..... My doubt is what...
9
2149
by: barcaroller | last post by:
Can variables be used for array size in C++? I know that in the past, I could not do the following: foo (int x) { type arr; } I have recently seen code that does exactly that. Is it right?
0
7490
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7935
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7449
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6009
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5351
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3479
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.