Creating functions sequentialy

S

Sergio

Hi, I was trying to implement functions with data associted to it
sequentialy, I tried the following way(below), but that does not comply
with the compiler norms for C++ even though that's not any semantic or
sintax error..., does any one knows a way to do that?

Thanks

Code:
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++)
/*error C2964: invalid expression as template parameter*/
functions=ClassA<&x>::p;

return 0;
}
 
M

mlimber

Sergio said:
Hi, I was trying to implement functions with data associted to it
sequentialy, I tried the following way(below), but that does not comply
with the compiler norms for C++ even though that's not any semantic or
sintax error..., does any one knows a way to do that?

Thanks

Code:
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++)
/*error C2964: invalid expression as template parameter*/
functions=ClassA<&x>::p;

return 0;
}


Template parameters must be constant at compile-time. Thus, you cannot
use a for loop variable to generate templates. You could use template
metaprogramming to generate that range for you, but your code above
borders on immoral in my book so I won't give an example. :p

Cheers! --M
 
S

Sergio

Thanks!!!

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

template<int N>
class ClassA{
int transition;
public:
ClassA()
{
ClassA<N-1>();
functions[N]=ClassA::p;
}
static void P()
{
cout<<N<<endl;
}
};

template<>
class ClassA<-1> {};
 

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,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top