How to test value of template parameter?

D

David Sanders

Hi,

I have a class with an integer template parameter, taking values 1, 2
or 3, and a function 'calc' in that class which performs
calculations. Some calculations need only be performed if the
template parameter is 2 or 3; for efficiency, I do not wish to perform
the calculations if the template parameter is 1.

I currently do this as follows:

template<int d>
class myclass {

myclass() {
}

void calc() {

// calculation for d==1 goes here -- performed also if d==2 or
d==3

if (d==2) ...
if (d==3) ...
}
};

My question is, how can I replace these tests with a compile-time test
of the value of the template parameter to include only the relevant
code? I have tried #if, but this does not see the value of the
template parameter.

As far as I understand, one solution would be to put the d==2 and d==3
cases in a separate function and use template specialization? But
this seems to be overkill in this simple case.

Thanks and best wishes,
David.
 
A

alan

Hi,

I have a class with an integer template parameter, taking values 1, 2
or 3, and a function 'calc' in that class which performs
calculations. Some calculations need only be performed if the
template parameter is 2 or 3; for efficiency, I do not wish to perform
the calculations if the template parameter is 1.

I currently do this as follows:

template<int d>
class myclass {

myclass() {
}

void calc() {

// calculation for d==1 goes here -- performed also if d==2 or
d==3

if (d==2) ...
if (d==3) ...
}

};

My question is, how can I replace these tests with a compile-time test
of the value of the template parameter to include only the relevant
code? I have tried #if, but this does not see the value of the
template parameter.

If compiler optimizations are turned on, the test will generally be
done at compile-time.
As far as I understand, one solution would be to put the d==2 and d==3
cases in a separate function and use template specialization? But
this seems to be overkill in this simple case.
It can be done that way, too.

Sincerely,
AmkG
 
B

Barry

David said:
Hi,

I have a class with an integer template parameter, taking values 1, 2
or 3, and a function 'calc' in that class which performs
calculations. Some calculations need only be performed if the
template parameter is 2 or 3; for efficiency, I do not wish to perform
the calculations if the template parameter is 1.

I currently do this as follows:

template<int d>
class myclass {

myclass() {
}

void calc() {

// calculation for d==1 goes here -- performed also if d==2 or
d==3

if (d==2) ...
if (d==3) ...
}
};

My question is, how can I replace these tests with a compile-time test
of the value of the template parameter to include only the relevant
code?

AFAIK, there is not.

I have tried #if, but this does not see the value of the
template parameter.

As far as I understand, one solution would be to put the d==2 and d==3
cases in a separate function and use template specialization? But
this seems to be overkill in this simple case.

I think a template parameter of class template is used to generate
generic data member types and generic member functions.

In your case, the "d" template parameter is only used in "calc" for a
if-else switch.

Ignoring the "overkilling" thing, I would like to suggest you have
template member function, which is more reasonable, and use what you
already mentioned -- "function template specialization". If this is
overkilling, I think it's already overkilling if you use template here.

<code>
class myClass {
public:
template <int d> void calc();
};

// MUST be specialized in the "myClass"s enclosing scope,
// MSVC has a bug here.
template <>
void myClass::calc<1>() {
}

template <>
void myClass::calc<2>() {
}

int main() {
myClass c;
c.calc<1>();
c.calc<2>();
}
</code>
 
D

David Sanders

If compiler optimizations are turned on, the test will generally be
done at compile-time.

Ah, I didn't realise that -- very good news, thanks!

Best wishes,
David.
 
D

David Sanders

I think a template parameter of class template is used to generate
generic data member types and generic member functions.

In your case, the "d" template parameter is only used in "calc" for a
if-else switch.

Ignoring the "overkilling" thing, I would like to suggest you have
template member function, which is more reasonable, and use what you
already mentioned -- "function template specialization". If this is
overkilling, I think it's already overkilling if you use template here.

OK, I guess template specialization really is the right thing to use
after all.
The reason I use a template is, as suggested in my original question,
that code efficiency is critical -- these functions
are called many times, so any way I can eliminate unnecessary code is
good.

<code>
class myClass {
public:
template <int d> void calc();

};

// MUST be specialized in the "myClass"s enclosing scope,
// MSVC has a bug here.
template <>
void myClass::calc<1>() {

}

template <>
void myClass::calc<2>() {

}

int main() {
myClass c;
c.calc<1>();
c.calc<2>();}

</code>

Thanks and best wishes,
David.
 

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
473,773
Messages
2,569,594
Members
45,114
Latest member
GlucoPremiumReview
Top