473,403 Members | 2,359 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,403 software developers and data experts.

numbers in templates

Hello,

I was wondering what the pros and cons are for using
numbers in templates rather than in constructors as in:

MyBuffer<10foo;

rather than

MyBuffer foo(10);

Thanks,

JG

Sep 3 '06 #1
4 1784
John Goche posted:
MyBuffer<10foo;

rather than

MyBuffer foo(10);

That question cannot be answered without context.

The former is more restrictive, in that you need a separate type for each
integral value.

--

Frederick Gotham
Sep 3 '06 #2
John Goche schrieb:
Hello,

I was wondering what the pros and cons are for using
numbers in templates rather than in constructors as in:

MyBuffer<10foo;

rather than

MyBuffer foo(10);
Think of MyBuffer as some kind of array/vector.

With MyBuffer<10you would have an array of fixed size. The size has to be
known at compile-time and has to be a compile-time constant, as with plain
arrays. Each template instantiation forms a new _type_ (the size is part of
the type), and this types are not interchangeable (you cannot pass a
MyBuffer<10where a MyBuffer<3is expected). This way is usefull, for
example, for vectors in maths, where it doesn't make sense to add a
3D-vector to a 4D-vector.

The size as constructor parameter can be a dynamic value, determined at
run-time. As in std::vector, the size can be changed later and is not part
of the type.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Sep 3 '06 #3
Thomas J. Gritzan wrote:
John Goche schrieb:
Hello,

I was wondering what the pros and cons are for using
numbers in templates rather than in constructors as in:

MyBuffer<10foo;

rather than

MyBuffer foo(10);

Think of MyBuffer as some kind of array/vector.

With MyBuffer<10you would have an array of fixed size. The size has to be
known at compile-time and has to be a compile-time constant, as with plain
arrays. Each template instantiation forms a new _type_ (the size is part of
the type), and this types are not interchangeable (you cannot pass a
MyBuffer<10where a MyBuffer<3is expected). This way is usefull, for
example, for vectors in maths, where it doesn't make sense to add a
3D-vector to a 4D-vector.
There seems to be a lot of overhead to this approach since a type has
to be created for each size. What are the performance benefits
associated with knowing the size at compile time, and how do these
outweigh the drawback of codesize bulk due to having to create
multiple types?

Thanks,

JG

Sep 4 '06 #4
John Goche schrieb:
There seems to be a lot of overhead to this approach since a type has
to be created for each size. What are the performance benefits
associated with knowing the size at compile time, and how do these
outweigh the drawback of codesize bulk due to having to create
multiple types?
This can't be answered in general so easily.
It depends, and should be measured when you have a concrete class.

Types by itself are no entities saved in you binary, but the functions
generated increase the size. For inlined functions, that should make no
difference.
It all depends on your compiler/linker, the optimization level, and of
course, your class design.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Sep 4 '06 #5

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

Similar topics

5
by: Tom Alsberg | last post by:
Hi there... I'm recently trying to get a bit acquainted with XML Schemas and XSL. Now, I have a few questions about XSL stylesheets and templates: * Is there a way to "enter" a child element...
21
by: Blair | last post by:
could someone PLEASE tell me why this doesn't work... ----------------------------------------- #include <complex> using namespace std; typedef complex<long double> cld; void main() { cld...
22
by: E. Robert Tisdale | last post by:
According to the C++ FAQ Lite: http://www.parashift.com/ What is "genericity"? Yet another way to say, "class templates." Not to be confused with "generality" (which just means avoiding...
19
by: Dan | last post by:
Some knows how to verify an ascii code. I want to verify a char for the list of ascii codes between 64 to 122. Mainly in other words, check a char, look if its in a range of ascii code and output...
25
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the...
5
by: soeren | last post by:
Hello, two days ago I stumbled across a very strange problem that came up when we were printing tiny double numbers as strings and trying to read them on another place. This is part of an object...
28
by: NewToCPP | last post by:
Hi, I am just trying to find out if there is any strong reason for not using Templates. When we use Templates it is going to replicate the code for different data types, thus increasing the...
12
by: begum | last post by:
in this program they want me to find the max value of a subsequence in a given sequnce of numbers(the sequence is represented by an array) by using templates and pointers.also i must write a main...
11
by: zj262144 | last post by:
Hi, I'm a C beginner. I want to write a C. The pseudocodes are like this: User enters some numbers (use -1 to end) The compiler find and delete the maximum and minimum numbers Then average...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...
0
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,...
0
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...

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.