C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Using templates with arrays

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Zara
Guest





PostPosted: Mon Sep 25, 2006 9:06 am    Post subject: Using templates with arrays Reply with quote



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
Back to top
Zara
Guest





PostPosted: Mon Sep 25, 2006 9:10 am    Post subject: Re: Using templates with arrays Reply with quote



On Mon, 25 Sep 2006 06:06:52 +0200, Zara <me_zara (AT) dea (DOT) spamcon.org>
wrote:

Quote:
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
Back to top
Rolf Magnus
Guest





PostPosted: Mon Sep 25, 2006 9:10 am    Post subject: Re: Using templates with arrays Reply with quote



Zara wrote:

Quote:
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.
Back to top
Guest






PostPosted: Mon Sep 25, 2006 9:10 am    Post subject: Re: Using templates with arrays Reply with quote

Zara wrote:
Quote:
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.

Quote:

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

Best regards,

Zara
Back to top
Zara
Guest





PostPosted: Mon Sep 25, 2006 9:10 am    Post subject: Re: Using templates with arrays Reply with quote

On Mon, 25 Sep 2006 07:32:26 +0200, Rolf Magnus <ramagnus@t-online.de>
wrote:

Quote:
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]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.