473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Partial spec and nontype parameters.

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.
Jul 23 '05 #1
3 1390
Amit wrote:
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
Jul 23 '05 #2
"Amit" <am***********@intel.com> schrieb im Newsbeitrag
news:d7**********@news01.intel.com...
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
Jul 23 '05 #3

"Arne Adams" <ar********@t-online.de> wrote in message
news:d7*************@news.t-online.com...
"Amit" <am***********@intel.com> schrieb im Newsbeitrag
news:d7**********@news01.intel.com...
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


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.
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I...
21
by: nospam | last post by:
Ok, I asked this question before and I also looked at the book "First Look at ASP.NET 2.0" I also read Paul wilson's web page explanation. HOWEVER...... The book and that web page talks about...
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
2
by: Michael Stembera | last post by:
Here is a very simple piece of code to repro this bug. template<typename T, int N> inline bool foo( void ) { return true; } template<typename T> inline bool foo<T, 1>( void ) { return...
6
by: rincewind | last post by:
Hi, can anybody summarise all options for partial template specialization, for all kind of parameters (type, nontype, template)? I *think* I understand options for partial specialization on...
6
by: year1943 | last post by:
For template <typename Tclass My ; I can define partial spec-ns somewhat like template <typename Tclass My<T*; or template <typename Tclass My<Another<T ; And full spec-n, say template <class...
7
by: esrever | last post by:
Can I create a specialization of a single nontype parameter of a templated function of a non-templated class?? For example : class A { ... template <int i> Handle( void * p) {} template <>...
10
by: jason.cipriani | last post by:
I never seem to be able to get this right. Here I have some code: template <typename T, int Nclass A { void f (T); }; template <typename Tvoid A<T,1>::f (T) { } template <typename Tvoid...
2
by: =?ISO-8859-1?Q?Karl_Tikj=F8b_Krukow?= | last post by:
Erwin Moller wrote: Don't worry other people have already done that ;-) The point of the project is to build a program specializer (= partial evaluator). In the case of Jeene, a program...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.