By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,193 Members | 843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.