Partial spec and nontype parameters.

A

Amit

Greetings.
How does one associate a integer variable to a non-type template parameter ?
Is there anyway of doing it in an easy way using partial specialization ? so
what I have is something to the effect of,

template<typename T, int N>
class X
{
bool operator()(T* p){
// do something;
}
}

template<typename T>
class X<T, 0>
{
bool operator()(T* p){
// do something;
}
}

template<typename T>
class X<T, 1>
{
bool operator()(T* p){
// do something;
}
}

and then using it like this,

template<typename T>
Myclass<T>::foo()
{
int val;
// evaluate val somehow..

X<T, val>()(someptr_p); //line 1
}

of course the compiler gives an error at line 1 as it expects a compile time
constant. So, is there anyway where one can evaluate val to something that
can be used
as a compile time constant.
Thanks.
 
V

Victor Bazarov

Amit said:
Greetings.
How does one associate a integer variable to a non-type template parameter ?
Is there anyway of doing it in an easy way using partial specialization ? so
what I have is something to the effect of,

template<typename T, int N>
class X
{
bool operator()(T* p){
// do something;
}
}

template<typename T>
class X<T, 0>
{
bool operator()(T* p){
// do something;
}
}

template<typename T>
class X<T, 1>
{
bool operator()(T* p){
// do something;
}
}

and then using it like this,

template<typename T>
Myclass<T>::foo()
{
int val;
// evaluate val somehow..

X<T, val>()(someptr_p); //line 1
}

of course the compiler gives an error at line 1 as it expects a compile time
constant. So, is there anyway where one can evaluate val to something that
can be used
as a compile time constant.

"Is there any way to have 'val' be a run-time object (and have its value
to only be determined at run-time) and then somehow use that value in the
past, at compile time, somehow, maybe?"

The answer is "Yes, just get the compiler/OS combination with the built-in
time machine".

On a more serious note, of course, it all depends on *how* "somehow" you
intend to "evaluate val" in the 'foo' member. If that evaluation can be
done at compile time, sure. Declare your 'val' "const int" and initialise
it with the constant expression evaluated at compile-time. Now you have
a compile-time constant that you can use as the second argument for your
template.

V
 
A

Arne Adams

Amit said:
Greetings.
How does one associate a integer variable to a non-type template parameter
?
Is there anyway of doing it in an easy way using partial specialization ?
so
what I have is something to the effect of,

You could use a function table or a switch statement:


template<typename T, int N>
class X
{
public:
bool operator()(T* p){
// do something;
return false;
}
};

template<typename T>
class X<T, 0>
{
public:
bool operator()(T* p){
return true;
}
};

template<typename T>
class X<T, 1>
{
public:
bool operator()(T* p){
return true;
};
};

template<int N, class T> bool xFunction(T* p)
{
return X<T,N>()(p);
}

template<class T> struct XFunctionTable
{
typedef bool(*Function)(T*);
enum {NumSpecializations = 5};
static bool validIndex(int);
static Function functions[NumSpecializations];
};

template<class T> bool XFunctionTable<T>::validIndex(int i)
{
return i >= 0 && i < NumSpecializations;
}

template<class T> typename XFunctionTable<T>::Function
XFunctionTable<T>::functions[NumSpecializations]
={&xFunction<0>,&xFunction<1>, &xFunction<2>, &xFunction<3>, &xFunction<4>};

template<typename T> struct MyClass
{
void foo1();
void foo2();
};

template<typename T> void
MyClass<T>::foo1()
{
int val;
val = 2; // evaluate val somehow
if(XFunctionTable<T>::validIndex(val))
{
T* someptr_p = 0;
XFunctionTable<T>::functions[val]( someptr_p);
}
else
;// some appropriate error handling.
}

template<typename T> void
MyClass<T>::foo2()
{
int val;
val = 1; // evaluate val somehow

T* someptr_p = 0;
switch(val)
{
case 0:
X<T, 0>()(someptr_p);
break;
case 1:
X<T, 1>()(someptr_p);
break;
case 2:
X<T, 2>()(someptr_p);
break;
case 3:
X<T, 3>()(someptr_p);
break;
case 4:
X<T, 4>()(someptr_p);
break;
default:
;// some appropriate error handling.
}
}


int main(int argc, char* argv[])
{
MyClass<int> test;
test.foo1();
test.foo2();
return 0;
}

Regards,
Arne
 
A

Amit

Arne Adams said:
Amit said:
Greetings.
How does one associate a integer variable to a non-type template parameter
?
Is there anyway of doing it in an easy way using partial specialization ?
so
what I have is something to the effect of,

You could use a function table or a switch statement:


template<typename T, int N>
class X
{
public:
bool operator()(T* p){
// do something;
return false;
}
};

template<typename T>
class X<T, 0>
{
public:
bool operator()(T* p){
return true;
}
};

template<typename T>
class X<T, 1>
{
public:
bool operator()(T* p){
return true;
};
};

template<int N, class T> bool xFunction(T* p)
{
return X<T,N>()(p);
}

template<class T> struct XFunctionTable
{
typedef bool(*Function)(T*);
enum {NumSpecializations = 5};
static bool validIndex(int);
static Function functions[NumSpecializations];
};

template<class T> bool XFunctionTable<T>::validIndex(int i)
{
return i >= 0 && i < NumSpecializations;
}

template<class T> typename XFunctionTable<T>::Function
XFunctionTable<T>::functions[NumSpecializations]
={&xFunction<0>,&xFunction<1>, &xFunction<2>, &xFunction<3>,
&xFunction said:
template<typename T> struct MyClass
{
void foo1();
void foo2();
};

template<typename T> void
MyClass<T>::foo1()
{
int val;
val = 2; // evaluate val somehow
if(XFunctionTable<T>::validIndex(val))
{
T* someptr_p = 0;
XFunctionTable<T>::functions[val]( someptr_p);
}
else
;// some appropriate error handling.
}

template<typename T> void
MyClass<T>::foo2()
{
int val;
val = 1; // evaluate val somehow

T* someptr_p = 0;
switch(val)
{
case 0:
X<T, 0>()(someptr_p);
break;
case 1:
X<T, 1>()(someptr_p);
break;
case 2:
X<T, 2>()(someptr_p);
break;
case 3:
X<T, 3>()(someptr_p);
break;
case 4:
X<T, 4>()(someptr_p);
break;
default:
;// some appropriate error handling.
}
}


int main(int argc, char* argv[])
{
MyClass<int> test;
test.foo1();
test.foo2();
return 0;
}

Regards,
Arne

Thanks very much for taking the time for the solution.
Having said that, in my case I only need to have three conditions where the
integer value can be either 0 or 1 or anything more than one. So, maybe i
need the switch statement going upto the max value I need ?
or else maybe I just if else or switch loop and call the functions directly.
Thanks anyway.
 

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

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top