Showing results for 
Search instead for 
Do you mean 

Template instantiation

Highlighted
Occasional Contributor

Template instantiation

Hi,

I am trying to compile STLPort 4.5.3 using aCC 3.55. HP-UX PA-RISC 2.0 11.11

It contains a code snippet which can be summarized as follows:

template
void GetMax(myType A){
int a[A];
cout<<"Done";
};


int main()
{
cout <<"Ashu";
GetMax(10);
cout <<"Kanu";
return 0;

}

so whenever I compile it using my compiler i receive the following error messages:

Error 176: "template.c", line 8 # Size of array must be an integral constant expression
of value greater than 0.
int a[A];
^
Error 445: "template.c", line 16 # Cannot recover from earlier errors.
GetMax(10);

How do I get rid of this message, so that I would be able to compile the STLPort?

Thanks
-Deo


2 REPLIES
Occasional Contributor

Re: Template instantiation

Please consider the following program:

# include
# define _STLP_DATA_ALIGNMENT 10
# define _Pthread_alloc_obj int
# define _STLP_mutex int
template
struct _Pthread_alloc_per_thread_state {
typedef _Pthread_alloc_obj __obj;
enum { _S_NFREELISTS = _Max_size/_STLP_DATA_ALIGNMENT };
_Pthread_alloc_obj* volatile __free_list[_S_NFREELISTS];
// Free list link for list of available per thread structures.
// When one of these becomes available for reuse due to thread
// termination, any objects in its free list remain associated
// with it. The whole struct thread.
_STLP_mutex _M_lock;

};

main()
{
/*iNT tp=5000;
size_t sz= sizeof(tp);
struct _Pthread_alloc_per_thread_state deo;
*/
printf ("Deo");
}

This compiles using CC on Solaris machine, however I am not able to compile the same on my HP machine

Thanks
-Deo
Acclaimed Contributor [Founder]

Re: Template instantiation

template
void GetMax(T A){ int a[A]; }
GetMax(10);

This is a dynamic array. If you want the size to be a constant, you need to do something like your next case with a non-type template parm.

>size_t sz = sizeof(tp);
>struct _Pthread_alloc_per_thread_state deo;

(It is silly to use scummy C struct/class in C++, drop the "struct".)

>This compiles using CC on Solaris machine

It's not Standard. You'll need to use:
const size_t sz = sizeof(tp);
_Pthread_alloc_per_thread_state deo;

>main()

This must be "int main".