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

Novice Question: Good example on use of Non-type template parameters?

Can anyone kindly explain why non-type template parameters are
required by giving some examples where their uses are clearly
favourable to other alternatives?

I cannot think of any good use for them except to create different
sizes of static arrays from a template, but this could be done by
creating a dynamic array of different sizes accepting the sizes in the
constructor thus not requiring the use of non-type template parameters
and the dynamic array approach seems to be a better choice to my eyes.
(It's just my feeling. I do not know which is better. But it's not the
important issue here).

I know my inability to think of good uses of non-type template
parameters is due to my lack of experience and if the feature is
included in the language, it must be there for a good reason. So can
anyone help me learn to use this feature properly? Thank you very much
in advance!

P.S I am begining to enjoy learning C++ but I sometimes find hard to
appreciate certain features. Is there any book which explains the
language features with realistic design and implementation examples,
not with those crafted examples just for the sake of explaining the
features? Thank you always!
Jul 19 '05 #1
2 2990
CoolPint escribió:
I cannot think of any good use for them except to create different
sizes of static arrays from a template, but this could be done by
creating a dynamic array of different sizes accepting the sizes in the
constructor thus not requiring the use of non-type template parameters
and the dynamic array approach seems to be a better choice to my eyes.
An object of a templated array class can be completely allocated in the
stack, using a dynamic array need memory allocation and deallocation. In
some cases that may be important.
I know my inability to think of good uses of non-type template
parameters is due to my lack of experience and if the feature is
included in the language, it must be there for a good reason.


There is no prize for use all the features of the language in a program.
Use only the features you find useful in each case.

Regards.
Jul 19 '05 #2
"CoolPint" <co******@yahoo.co.uk> wrote in message
news:15**************************@posting.google.c om...
Can anyone kindly explain why non-type template parameters are
required by giving some examples where their uses are clearly
favourable to other alternatives?
** For template parameters of integral type:

What about a graphic/geometry package that provides a single
template implementation of a position vector:
template< class S /*scalar*/ , unsigned D /*dim*/ >
class Vec {
...
private:
S m[D];
};
typedef Vec<float,3> vec3f;
typedef Vec<float,2> vec2f;

While dynamic/run-time sizing of vector would be an option,
defining the size as a template parameter has strong benefits:
- in terms of memory allocation for small vectors
- in terms of type safety: an error can be reported
at compile-time if a vec3f is assigned to a vec2f.
** For template parameters of variable type

Using a pointer to a member function is useful in callback
objects.

P.S I am begining to enjoy learning C++ but I sometimes find hard to
appreciate certain features. Is there any book which explains the
language features with realistic design and implementation examples,
not with those crafted examples just for the sake of explaining the
features? Thank you always!


One idea could be to study real-world libraries and frameworks
that take advantage of these features.
Take the boost libraries (www.boost.org), and study the implementation
of smart pointers, of the boost graph library (dedicated book exists),
of iterator adaptors, etc.
Also, "Modern C++ Design" by Andrei Alexandrescu shows extreme examples
of how some C++ features can be used. Believe it or not, Andrei claims
to be using the described techniques in production code ...
(see www.moderncppdesign.com).

hth -Ivan
--
http://ivan.vecerina.com


Jul 19 '05 #3

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

Similar topics

21
by: AES/newspost | last post by:
My understanding -- I'm not an expert -- is that on (some? many? all?) standard Internet servers a URL can point to a subdirectory name followed by a backslash, and that links to this URL will...
4
by: OneSolution | last post by:
I am trying to write a regular expression that will match all the exceptions generated in my application log file. I have done it as such: \bjava\.\w+\.\w+Exception: However, there are two...
3
by: herrcho | last post by:
Here is the code.. #define NAME "MEGATHINK, INC" #define ADDRESS "10 Megabuck Plaza" #define PLACE "Megapolis, CA 94904" int main() { starbar(); printf("%s\n",NAME);
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
5
by: local | last post by:
Being new to C#.NET I wondered if it were possible to compile a program to run on a machine which does not have the .NET framework installed?
7
by: gordon | last post by:
Hi I am learning C# from books. I am trying to understand the use of the word 'Static' on a method. When i look in the autos area (I often like to see what values are being resolved) i see...
25
by: gordon | last post by:
I aksed a few days ago about static methods and got some good answers that were really useful. Now I am not sure if about the use of static on members (variables) and classes. Can someone...
90
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
5
by: runa2104 | last post by:
Interview Question Visit this site it is really good question on C language. http://techinterviewquestion.blogspot.com/2007/05/c-question.html
3
by: Chris Frost | last post by:
Hello all, I had a look through the FAQ and searched this forum before posting. The closest I came to an answer was this post by Dr J R Stockton from 2006. But as he/she said, the answer depends on...
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.