Getting the length of an array. Compiler errors.
Asked Answered
O

2

2

I've tried:

template <typename T,unsigned S> 
unsigned getArraySize(const T (&v)[S]) { return S; } 

after Motti's answer https://mcmap.net/q/63572/-how-do-i-find-the-length-of-an-array

but I've got this message:

error C2265: '' : reference to a zero-sized array is illegal

What's wrong with my compiler?

I gave a look at this page: http://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/4b78bcef-4c33-42f1-a4c5-fb6f702ced0b/vs6-c-compile-error-using-getaddrinfo so I tried this solution:

template <typename T,unsigned S> 
unsigned getArraySize(const T v[S]) { return S; } 

this compiles, but when I try to use it:

double myX[2] = {7,3};
std::cout << getArraySize(myX) << std::endl; 

I get a compilation error:
error C2783: 'unsigned int __cdecl getArraySize(const T [])' : could not deduce template argument for 'S'

Beside changing the compiler, is there a workaround I can use to get the array's size?

Openmouthed answered 28/8, 2013 at 7:48 Comment(15)
Use std::array. What is the error when you do getArraySize(myX) with the first definition? The error implies a zero length array, but your array myX obviously has more than zero elements.Bothy
Can you post the code that is giving you C2265 for the first solution?Bechtel
The first example should work... I literally copy/pasted it and ran it and it returned 2. What compiler do you have?Sperling
Did you try this on a more recent msvc compiler? vc6 is at least a decade old.Monodic
Your first version should work. Can you post an SSCCE?Bruch
You might not pass a 'const T []', a 'const T [1]' should work.Indiscipline
@Monodic More like 2 decades. 10 years ago, there was VS 2003.Bechtel
@Bothy my company is using VC6, I think std::array has recently been added to the standard library.Openmouthed
@Angew the first solution in an included header file gives the error (even if the template function is never called) i.e. there's no more code than these two linesOpenmouthed
@Monodic I'm pretty sure that on a more recent compiler it works, I'll try at homeOpenmouthed
@Bruch my example is short, self contained, correct (according to most compilers) but of course cannot compile (that's the reason why I ask a question, isn't it)? :-)Openmouthed
@Openmouthed It would have been easier to rule out other errors if you had posted something like the demo linked from my last comment.Bruch
@Openmouthed I guess the only answer here is "update to a more recent compiler, or at least to one which at least pretends to care about the standard." VC++6 was never one for following the standard, especially with templates. Can't really blame it, it was released in the same year as the first C++ standard, but really is not a C++ compiler.Bechtel
@Openmouthed If updating isn't an option for whatever reason, you'll just have to fallback to the old C method sizeof(array) / sizeof(array[0]) as a workaround. But don't worry! Macros can help here! :DMonodic
These are my accepted answers -the only answer here is "update to a more recent compiler, -If updating isn't an option for whatever reason, you'll just have to fallback to the old C method.Openmouthed
S
2

This could be a limitation of VC6, have you tried other compilers?

Sauder answered 28/8, 2013 at 11:53 Comment(0)
C
1

but I've got this message:

error C2265: '' : reference to a zero-sized array is illegal

Arrays with zero size are illegal in C++.
So this probably means you tried with an array of zero size.

this compiles, but when I try to use it:

If you don't use it. Then the compiler is going to ignore a template (even if it has errors). This is because you can not always deduce if a template function is correct without knowing the types involved. So unless there is a call to a template function no error message will be generated.

template <typename T,unsigned S>
unsigned getArraySize(const T v[S]) { return S; }

This fails because you are not allowed to pass arrays as parameters (you can only pass references to arrays).

Camlet answered 28/8, 2013 at 9:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.