473,289 Members | 1,842 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,289 software developers and data experts.

How to test value of template parameter?

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.
Nov 18 '07 #1
4 2991
On Nov 18, 1:04 pm, David Sanders <dpsand...@gmail.comwrote:
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
Nov 18 '07 #2
David Sanders wrote:
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 dvoid 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>

Nov 18 '07 #3
On Nov 18, 3:27 am, alan <almkg...@gmail.comwrote:
On Nov 18, 1:04 pm, DavidSanders<dpsand...@gmail.comwrote:
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.
Ah, I didn't realise that -- very good news, thanks!

Best wishes,
David.

Nov 19 '07 #4
On Nov 18, 5:29 am, Barry <dhb2...@gmail.comwrote:
David Sanders wrote:
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.
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 dvoid 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.
Nov 19 '07 #5

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

Similar topics

3
by: Colin Toal | last post by:
Hi all, I'm starting to learn XSLT - and have what I think is a very basic question: I have a stylesheet like this: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet...
4
by: Pat Turner | last post by:
Hi, I have some XML like this: <family> <person name="bob"> <father ref="../../person" /> </person> <person name="charlie"> <child ref="../../person" />
5
by: kmunderwood | last post by:
I am trying to combine "if match=" and "when test" I am a newbie, and have made both work separately, but I can not seem to combine them. This is my xml("index.xml")page(I can not change this,...
7
by: Robert Allan Schwartz | last post by:
Why do I get a syntax error below? I don't see why volatile works but unsigned does not work. I'm not looking for an answer of the form, "Because the Standard says so", or "Because the C++...
2
by: Siegfried Weiss | last post by:
Hi guys, i give up finding a solution by reading or by trial & error. Hope, YOU can help me! (Sorry for my rather long posting.) Stroustrup says, that templates could be declared with - type...
4
by: =?Utf-8?B?QmFidU1hbg==?= | last post by:
Hi, I have a GridView and a SqlDataSource controls on a page. The SqlDataSource object uses stored procedures to do the CRUD operations. The DataSource has three columns one of which -...
1
by: aidy | last post by:
Hi I have some xml that is similar to this <testresults> <test id="test_1"> <description>demo test method 1</description> <teststatus>PASS</teststatus> </test> <test id="test_2">
7
by: neelsmail | last post by:
Hi, I want to give default value as NULL/0 for non-type template parameter. I using SunStudio on Linux. I have tried following: #define non_closer ((int(*)(FILE*))0L) template<class T, int...
6
by: Ahmedhussain | last post by:
Hi there, I m doing work on a gridview and Im getting an error: A potentially dangerous Request.Form value was detected from the client (ctl00$Content$GridView1$ctl03$TextBox1="<span...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.