Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Using templates with arrays

Reply
Thread Tools

Using templates with arrays

 
 
Zara
Guest
Posts: n/a
 
      09-25-2006
I have been trying to substitute the macro that comes with the
compiler with the following template:

template <typename ITEM,size_t SIZE>
inline size_t arraySize(ITEM const (&array)[SIZE]) {
return SIZE;
}

AFAIK, it is compliant with the standard, and it should work.

In fact, it works perfectly on Borland C++ 2006, but it fails on GCC
3.4.1.

Should it work, from the Standard point of view, or am i missing
something?

Best regards,

Zara
 
Reply With Quote
 
 
 
 
amparikh@gmail.com
Guest
Posts: n/a
 
      09-25-2006

Zara wrote:
> I have been trying to substitute the macro that comes with the
> compiler with the following template:
>
> template <typename ITEM,size_t SIZE>
> inline size_t arraySize(ITEM const (&array)[SIZE]) {
> return SIZE;
> }
>
> AFAIK, it is compliant with the standard, and it should work.
>
> In fact, it works perfectly on Borland C++ 2006, but it fails on GCC
> 3.4.1.


The code is fine and it is a very common teqnique used for getting size
of an array.

>
> Should it work, from the Standard point of view, or am i missing
> something?
>
> Best regards,
>
> Zara


 
Reply With Quote
 
 
 
 
Zara
Guest
Posts: n/a
 
      09-25-2006
On Mon, 25 Sep 2006 06:06:52 +0200, Zara <(E-Mail Removed)>
wrote:

>I have been trying to substitute the macro that comes with the
>compiler with the following template:
>
>template <typename ITEM,size_t SIZE>
> inline size_t arraySize(ITEM const (&array)[SIZE]) {
> return SIZE;
> }

<..>
>In fact, it works perfectly on Borland C++ 2006, but it fails on GCC
>3.4.1.
>


So sorry for this comment, it works on both. The problem comes from
using an empty intilizer list for the array:

int data[]={};

This compiles on GCC and not on BCB, but after reading the relevant
part of C++Std, it should not compile (8.5.1-4 and footnote 91). My
fault.

Regrads,

Zara
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      09-25-2006
Zara wrote:

>>In fact, it works perfectly on Borland C++ 2006, but it fails on GCC
>>3.4.1.
>>

>
> So sorry for this comment, it works on both. The problem comes from
> using an empty intilizer list for the array:
>
> int data[]={};
>
> This compiles on GCC and not on BCB, but after reading the relevant
> part of C++Std, it should not compile (8.5.1-4 and footnote 91).


My guess is that you forgot to turn on all the warning options for gcc.

 
Reply With Quote
 
Zara
Guest
Posts: n/a
 
      09-25-2006
On Mon, 25 Sep 2006 07:32:26 +0200, Rolf Magnus <(E-Mail Removed)>
wrote:

>Zara wrote:
>
>>>In fact, it works perfectly on Borland C++ 2006, but it fails on GCC
>>>3.4.1.
>>>

>>
>> So sorry for this comment, it works on both. The problem comes from
>> using an empty intilizer list for the array:
>>
>> int data[]={};
>>
>> This compiles on GCC and not on BCB, but after reading the relevant
>> part of C++Std, it should not compile (8.5.1-4 and footnote 91).

>
>My guess is that you forgot to turn on all the warning options for gcc.


[OT]
I do use:

-pedantic -pedantic-errors -Wall -Werror -std=c++98

It seems ther is no diagnostic for this error.

[OT]

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      09-25-2006
Zara wrote:

>>> So sorry for this comment, it works on both. The problem comes from
>>> using an empty intilizer list for the array:
>>>
>>> int data[]={};
>>>
>>> This compiles on GCC and not on BCB, but after reading the relevant
>>> part of C++Std, it should not compile (8.5.1-4 and footnote 91).

>>
>>My guess is that you forgot to turn on all the warning options for gcc.

>
> [OT]
> I do use:
>
> -pedantic -pedantic-errors -Wall -Werror -std=c++98
>
> It seems ther is no diagnostic for this error.


Then it's the version. I'm using 4.1.2, and with -pedantic, it says:

error: zero-size array ‘data’

> [OT]


 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      09-25-2006
Zara <(E-Mail Removed)> wrote:
> I have been trying to substitute the macro that comes with the
> compiler with the following template:
>
> template <typename ITEM,size_t SIZE>
> inline size_t arraySize(ITEM const (&array)[SIZE]) {
> return SIZE;
> }
>
> AFAIK, it is compliant with the standard, and it should work.
>
> In fact, it works perfectly on Borland C++ 2006, but it fails on GCC
> 3.4.1.
>
> Should it work, from the Standard point of view, or am i missing
> something?


I see you found that your problem was with a size-0 array, but this
article by Alf P. Steinbach has a couple other approaches, along with
their pro's and con's:

http://home.no.net/dubjai/win32cpptu...y_size.doc.pdf

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
 
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
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Difference between accessing arrays and associative arrays using a int index shashi Perl Misc 17 04-13-2006 03:48 PM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM
using templates in templates John Harrison C++ 8 07-31-2003 12:00 PM



Advertisments