Compiling error

S

Sergio

Hi, here goes a very simple code, to me looks like everything is right,
but I see an error compiling with VC6..., does anyone has a clue for
what's wrong?

Thanks

template<int *INT_POINTER>
class ClassA
{
public:
static void P(){}
};

int a;
int x[3];

int main()
{
/*OK*/
ClassA<&a>::p();
/*error C2964: invalid expression as template parameter*/
ClassA<&x[0]>::p();

return 0;
}
 
R

realfun

ClassA<x>::p(); /*OK*/

cout << (int)x << endl;
cout << (int)(&x[0]) << endl;

the value it the same.

I am wondering there are some tricky thing about template parameter
checking.
 
G

Greg

ClassA<x>::p(); /*OK*/

cout << (int)x << endl;
cout << (int)(&x[0]) << endl;

the value it the same.

I am wondering there are some tricky thing about template parameter
checking.

Only a pointer to an object or function with external linkage can be
used as a non-type template parameter. And although x has external
linkage; &x[0] is a calculated address based on x, but it is not its
own externally-linked symbol.

Part of the reason for this restriction is to help the compiler
recognize identical template instantiations in different translation
units. Allowing arbitrary pointer template parameters would make that
task quite complicated.

Greg
 
S

Sergio

What I wanted to do is to create sequentialy functions that has data
associated with it and has parameters that are not under my control,
like the example below, does anyone knows another way to do that? Is
there any way to create sequentialy many copies of a certain function?

Thanks

template<int *INT_POINTER>
class ClassA
{
public:
static void P(){}
};

int x[3];
typedef void(*VOIDFUNCVOID)() ;
VOIDFUNCVOID functions[3];

int main()
{
for(int i=0;i<3;i++)
functions=ClassA<&x>::p();

return 0;
}
 
S

Sergio

Sorry, the right code for the example should be...

#include <iostream.h>

template<int *INT_POINTER>
class ClassA
{
public:
static void P(){}
};

int x[3];
typedef void(*VOIDFUNCVOID)() ;
VOIDFUNCVOID functions[3];

int main()
{
for(int i=0;i<3;i++)
functions=ClassA<&x>::p;

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top