Watch, Follow, &
Connect with Us
Public Report
Report From: Delphi-BCB/Compiler/C++/Front End/Language    [ Add a report in this area ]  
Report #:  54835   Status: Closed
sometimes when C-array is used in templates its size can be set only with literal
Project:  C++Builder Build #:  10.0.2166.28377
Version:    10.0 Submitted By:   Sergiy Kanilo
Report Type:  Crash / Data loss / Total failure Date Reported:  11/14/2007 10:45:42 AM
Severity:    Commonly encountered problem Last Updated: 3/20/2012 2:24:39 AM
Platform:    All versions Internal Tracking #:   256430
Resolution: Fixed (Resolution Comments) Resolved in Build: : 12.0.3140.16150
Duplicate of:  None
Voting and Rating
Overall Rating: (1 Total Rating)
5.00 out of 5
Total Votes: 15
Description
one of the simple ways to get information in compile time is to return a reference to array of different size and take its sizeof,
for example

template<typename T>
char (&f(T const&))[1];

unfortunately in BDS2006 that could cause internal compiler error if the size of array is anyhing but literal

enum
{

Q =2
};
int const P =2;

template<typename T> char ( &f( T& ) )[ 2 ]; //1
//template<typename T> char ( &f( T& ) )[ 1+1 ]; //2
//template<typename T> char ( &f( T& ) )[ Q ]; //3
//template<typename T> char ( &f( T& ) )[ P ]; //4

int main(int argc, char* argv[])
{

int v;

int p =sizeof( f( v ) );
}

compiles and runs fine, when array size is literal

comment line //1 and uncomment line //2 - array size is expression
compilations gives
[C++ Fatal Error] Unit1.cpp(15): F1004 Internal compiler error at ...

the same errors when size is enum and constant (line //3 and //4)

Steps to Reproduce:
Expression:
bcc32 -c qc54835a.cpp

Exp: Clean compile
Act:
Fatal F1004 qc54835c.cpp 15: Internal compiler error at 0x43e8ad with base 0x400000 in function main(int,char *)
Fatal F1004 qc54835c.cpp 15: Internal compiler error  in function main(int,char*)

with enum:
bcc32 -c qc54835b.cpp
same results

with const:
bcc32 -c qc54835c.cpp
same results
Workarounds
wrap array in structure

template<int N>
struct AN
{
  char array[N];
};

template<typename T> AN<1+1> f( T& );

int main(int argc, char* argv[])
{

int v;

int p =sizeof( f( v ).array );
}
Attachment
qc54835.zip
Comments

None

Server Response from: ETNACODE01