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

C

CoolPint

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!
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

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.
 
I

Ivan Vecerina

CoolPint said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top