473,549 Members | 5,156 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3005
On Nov 18, 1:04 pm, David Sanders <dpsand...@gmai l.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 "overkillin g" 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<dp sand...@gmail.c omwrote:
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 "overkillin g" 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
7381
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 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
4
3438
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
2063
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, it comes to me this way. <?xml version="1.0" encoding="iso-8859-1" ?>
7
1526
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++ grammer says so"; I'm looking for an explanation of *why* the Standard and/or the grammar say so. Thanks,
2
2749
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 parameter, e.g. template<class T> - parameters with *simple* types like int, e.g. template<int size> - template parameter, e.g. template<...
4
7221
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 - "Modified" of type DateTime - is hidden since it should not be edited by a user. The system handles the update for this column. So, I have hidden...
1
1442
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
3265
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 F(FILE*) = non_closer> but compiler throws error:
6
4257
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 class='txtColo...") Im using an access database. and when ever I try to update this it doesnt work. <%@ Page Language="C#" MasterPageFile="Mysite.master"...
0
7723
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7962
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7480
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7814
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6050
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5373
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3504
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3486
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
769
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.