473,748 Members | 10,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Code bloating when choosing template argument based on cmdline input

Hi,

I have two questions.

1. Are there any good way to generalize the code for switch statement
with any number of cases?

2. Suppose test() is a very big function, and there are many cases in
the switch statement. The binary code could be huge. Are there any
method to deal with this problem.

Note: I have to path N as a template argument because the compiler can
optimize test much better than if I pass N as a function argument.

Thanks,
Peng

#include <iostream>
#include <cstdlib>

template <int N>
void test() {
std::cout << N << std::endl;
//Can be a very long function.
}

int main(int argc, char *argv[]) {
int n = atoi(argv[1]);
switch(n) {
case 0: test<0>(); break;
case 1: test<1>(); break;
case 2: test<2>(); break;
case 3: test<3>(); break;
case 4: test<4>(); break;
case 5: test<5>(); break;// Can be much more than 5 cases
default:
std::cout << "not found" << std::endl;
}
}

Jul 3 '07 #1
4 1627
On Jul 3, 9:46 am, "PengYu...@gmai l.com" <PengYu...@gmai l.comwrote:
Hi,

I have two questions.

1. Are there any good way to generalize the code for switch statement
with any number of cases?
There are good ways and 'good' differs from case to case. Figure out
the commonality and put them in a function. The variations in terms of
inputs to be operated on can be passed as arguments.
2. Suppose test() is a very big function, and there are many cases in
the switch statement. The binary code could be huge. Are there any
method to deal with this problem.
Yes many instantiations of a template can lead to code bloat, the
object code will be huge. Find an alternate way.
example, you have a queue implementation where the element type is a
template param, so long as you do not have many instantiations like
200 odd, its fine. But if you have more than that then change your
design.
Note: I have to path N as a template argument because the compiler can
optimize test much better than if I pass N as a function argument.

Thanks,
Peng

#include <iostream>
#include <cstdlib>

template <int N>
void test() {
std::cout << N << std::endl;
//Can be a very long function.

}

int main(int argc, char *argv[]) {
int n = atoi(argv[1]);
switch(n) {
case 0: test<0>(); break;
case 1: test<1>(); break;
case 2: test<2>(); break;
case 3: test<3>(); break;
case 4: test<4>(); break;
case 5: test<5>(); break;// Can be much more than 5 cases
default:
std::cout << "not found" << std::endl;
}

}
Consider this:
#include <iostream>
#include <cstdlib>
void test(int N) {
std::cout << N << std::endl;
//Can be a very long function.
// I guess you either do not need N here or will be doing some
operations on N.
//
}

int main(int argc, char *argv[]) {
int n = atoi(argv[1]);
switch(n) {
case 0: test(0); break;
case 1: test(1); break;
case 2: test(2); break;
case 3: test(3); break;
case 4: test(4); break;
case 5: test(5); break;// Can be much more than 5 cases
default:
std::cout << "not found" << std::endl;
}
}

HTH
---
Regards,
Taran

Jul 3 '07 #2
On Jul 3, 4:55 am, Taran <taran.tripa... @gmail.comwrote :
On Jul 3, 9:46 am, "PengYu...@gmai l.com" <PengYu...@gmai l.comwrote:
Hi,
I have two questions.
1. Are there any good way to generalize the code for switch statement
with any number of cases?

There are good ways and 'good' differs from case to case. Figure out
the commonality and put them in a function. The variations in terms of
inputs to be operated on can be passed as arguments.
2. Suppose test() is a very big function, and there are many cases in
the switch statement. The binary code could be huge. Are there any
method to deal with this problem.

Yes many instantiations of a template can lead to code bloat, the
object code will be huge. Find an alternate way.
example, you have a queue implementation where the element type is a
template param, so long as you do not have many instantiations like
200 odd, its fine. But if you have more than that then change your
design.
Note: I have to path N as a template argument because the compiler can
optimize test much better than if I pass N as a function argument.
Thanks,
Peng
#include <iostream>
#include <cstdlib>
template <int N>
void test() {
std::cout << N << std::endl;
//Can be a very long function.
}
int main(int argc, char *argv[]) {
int n = atoi(argv[1]);
switch(n) {
case 0: test<0>(); break;
case 1: test<1>(); break;
case 2: test<2>(); break;
case 3: test<3>(); break;
case 4: test<4>(); break;
case 5: test<5>(); break;// Can be much more than 5 cases
default:
std::cout << "not found" << std::endl;
}
}

Consider this:

#include <iostream>
#include <cstdlib>

void test(int N) {
std::cout << N << std::endl;
//Can be a very long function.
// I guess you either do not need N here or will be doing some
operations on N.
//
}
In fact, I used this approach. But it is not as efficient as the
templated version of "test" function. The runtime difference can be a
few times.

It seems there is no good solution to this problem. I have to use the
templated version of "test", which is not flexible to accept arbitrary
arguments.
int main(int argc, char *argv[]) {
int n = atoi(argv[1]);
switch(n) {
case 0: test(0); break;
case 1: test(1); break;
case 2: test(2); break;
case 3: test(3); break;
case 4: test(4); break;
case 5: test(5); break;// Can be much more than 5 cases
default:
std::cout << "not found" << std::endl;
}
}

HTH
---
Regards,
Taran

Jul 3 '07 #3
Pe*******@gmail .com wrote:
Hi,

I have two questions.

1. Are there any good way to generalize the code for switch statement
with any number of cases?
no. You have to state explicitly the parameters in the template version,
because for each different template parameter set a different class and
different code will be instantiated.
2. Suppose test() is a very big function, and there are many cases in
the switch statement. The binary code could be huge. Are there any
method to deal with this problem.
No. A different code will be generated for each class at compile time.
Note: I have to path N as a template argument because the compiler can
optimize test much better than if I pass N as a function argument.
well, of course, because a lot of choices can be done at compile-time.
Remember, however, that this approach limits by far the flexibility of
the system. The major drawback is that you have to fix all the
parameters at compile time. Otherwise, optimise the code in another way,
usually this can lead to very good results as well.
Thanks,
Peng

#include <iostream>
#include <cstdlib>

template <int N>
void test() {
std::cout << N << std::endl;
//Can be a very long function.
}
In this example, the performance difference should be negligible. Are
you sure that your optimisations depend on the template and you are not
omitting some important optimisation consideration that is not related
with the templates?

Regards,

Zeppe

Jul 3 '07 #4
Pe*******@gmail .com wrote:
Hi,

I have two questions.

1. Are there any good way to generalize the code for switch statement
with any number of cases?
Good? Not really. The template has to be instantiated for all values that
you need. You can ask the compiler to do that, but the compiler will not be
happy:
#include <cstddef>
#include <iostream>

template <int N>
void test() {
std::cout << N << std::endl;
//Can be a very long function.
}

template < typename ArithmeticType,
ArithmeticType first, ArithmeticType last >
void
bin_search ( ArithmeticType val ) {
if ( first == last ) {
test<first>();
return;
}
if ( ( first + ( last-first )/2 ) < val ) {
bin_search< ArithmeticType, ( last - ( last-first )/2), last >( val );
} else {
bin_search< ArithmeticType, first, ( first + ( last-first )/2) >( val );
}
}

int main(int argc, char *argv[]) {
unsigned char n = atoi(argv[1]);
bin_search<unsi gned char, 0,-1>( n );
}

Expect your compiler to get mad if you try this for unsigned long instead of
unsigned char.

2. Suppose test() is a very big function, and there are many cases in
the switch statement. The binary code could be huge. Are there any
method to deal with this problem.
Yes: don't make N a template parameter.
Note: I have to path N as a template argument because the compiler can
optimize test much better than if I pass N as a function argument.
Oh well, never mind.
Best

Kai-Uwe Bux
Jul 3 '07 #5

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

Similar topics

67
4276
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. Unfortunately these ad hominem rhetorts are frequently introduced into purely technical discussions on the feasibility of supporting such functionality in C++. That usually serves to divert the discussion from the technical subject to a discussion of the...
8
11366
by: vpadial | last post by:
Hello, I want to build a library to help exporting c++ functions to a scripting languagge. The scripting language provides a function to register functions like: ANY f0() ANY f1(ANY) ANY f2(ANY, ANY) ANY f3(ANY, ANY, ANY)
6
4006
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual as well. To support thin-template, I need to make virtual function as inline. Now, I know that compiler would generate an out-of-line copy when it
19
2713
by: jameso321 | last post by:
Hi, We run an MS Access 2000 DB with about 15 users. It is on a Win 2000 Server (SP4) machine and runs through Citrix Metaframe Presentation Server 3.0. --------------------------
171
7768
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles) like it because you can split the code from the design. That is logical. But if you are the only one working on the code, it seem a little overkill. I use Dreamweaver to do my design and find it a bit of a hassle to have multiple files open...
14
2036
by: Gary Wessle | last post by:
Hi I wrote this routine below but getting the error below it which I don't know how to fix. thanks //useful.h /**
7
1506
by: Bo Yang | last post by:
Hi , I am reading some boost code now , and I got confused by the following code in the add_reference type trait . template <class TT&(* is_reference_helper1(wrap<T>) )(wrap<T>); char is_reference_helper1(...); template <class Tno_type is_reference_helper2(T&(*)(wrap<T>)); yes_type is_reference_helper2(...);
7
12587
by: gretean | last post by:
I have a problem that's driving me crazy involving Microsoft's ability to deduce template parameters. I am using Visual Studio .NET (aka VC7?), and it gives an error compiling the following code. template <int x, int yclass M { public: template <int xRet, int yRet, int xR, int yR> M<xRet, yRetoperator *(const M<xR, yR&b) const { M<xRet,yReta; return a;
25
2145
by: David Sanders | last post by:
Hi, As part of a simulation program, I have several different model classes, ModelAA, ModelBB, etc., which are all derived from the class BasicModel by inheritance. model to use, for example if the parameter model_name is "aa", then choose ModelAA. Currently I do this as follows:
0
8830
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9370
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9321
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4602
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2215
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.