473,320 Members | 2,104 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,320 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 2992
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.