Policies, traits, generics

J

Jon Slaughter

I'm reading Alexandrescu's Modern C++ design about the concept of policies.
What I'm wondering is if policies are a common design pattern in C++(OOP)
nowdays? It seems like a good idea but I'm having a little trouble following
along and I'm wondering if I should put in the time to really understand it
or if its just a waste because there are better things out there.

Like all these things about traits, generics, policies, etc don't mean
anything to me. I just know the very basics of templates but I'm trying to
learn more but not sure what direction to go. I don't want to spend much
time learning something that either is not used or won't be used for very
long.

Is there any good reference that discusses the ideas of these different(?)
concepts and compares them using simple language(not one that assumes I
already know about them)?

Thanks,

Jon
 
M

mlimber

Jon said:
I'm reading Alexandrescu's Modern C++ design about the concept of policies.
What I'm wondering is if policies are a common design pattern in C++(OOP)
nowdays? It seems like a good idea but I'm having a little trouble following
along and I'm wondering if I should put in the time to really understand it
or if its just a waste because there are better things out there.

Like all these things about traits, generics, policies, etc don't mean
anything to me. I just know the very basics of templates but I'm trying to
learn more but not sure what direction to go. I don't want to spend much
time learning something that either is not used or won't be used for very
long.

Is there any good reference that discusses the ideas of these different(?)
concepts and compares them using simple language(not one that assumes I
already know about them)?

I tried reading _Modern C++ Design_ when it first came out, and it went
over my head. Then I read Stroustrup's _C++PL_ (well, sections of it --
especially relevant are those chapters on templates and the standard
library), and when I returned to _MC++D_, it was a transformative
experience because I thought I had a good grasp of the language but
Alexandrescu did things that I never even thought were possible and
showed me some truly useful techniques (chief among them are his
abstract factories from chapter 8).

As far as policy-based design, there's more to _MC++D_ than just that,
and I'd encourage you to read on. Perhaps you could do what I did: skim
chapters 1-3, which describe the tools he'll use in the rest of the
book, then read chapters 8 and 9 on factories and chapter 7 on smart
pointers, and return to chapters 1-3 to better understand the tools he
is using when it becomes necessary and when you can see the direct
application. It's also handy to have the Gang of Four's _Design
Patterns_ at the ready because _MC++D_ refers to their material a
goodly number of times.

There aren't too many policy-based libraries out there, but the Boost
libraries are a prominent example of some that make use of the
technique (though, they explicitly declined to use a policy-based
approach with their smart pointers). I think Microsoft's Active
Template Library also makes use of policy-based design, but I could be
wrong. Of course the C++ standard library itself makes use of traits
and generics.

Cheers! --M
 
J

Jon Slaughter

mlimber said:
I tried reading _Modern C++ Design_ when it first came out, and it went
over my head. Then I read Stroustrup's _C++PL_ (well, sections of it --
especially relevant are those chapters on templates and the standard
library), and when I returned to _MC++D_, it was a transformative
experience because I thought I had a good grasp of the language but
Alexandrescu did things that I never even thought were possible and
showed me some truly useful techniques (chief among them are his
abstract factories from chapter 8).

As far as policy-based design, there's more to _MC++D_ than just that,
and I'd encourage you to read on. Perhaps you could do what I did: skim
chapters 1-3, which describe the tools he'll use in the rest of the
book, then read chapters 8 and 9 on factories and chapter 7 on smart
pointers, and return to chapters 1-3 to better understand the tools he
is using when it becomes necessary and when you can see the direct
application. It's also handy to have the Gang of Four's _Design
Patterns_ at the ready because _MC++D_ refers to their material a
goodly number of times.

There aren't too many policy-based libraries out there, but the Boost
libraries are a prominent example of some that make use of the
technique (though, they explicitly declined to use a policy-based
approach with their smart pointers). I think Microsoft's Active
Template Library also makes use of policy-based design, but I could be
wrong. Of course the C++ standard library itself makes use of traits
and generics.

Cheers! --M

Ok, I'm going to go ahead and see what I can learn about traits and generics
for so I have some idea. Hopefully I find a good resource out there that can
give me a decent over view of them without taking to much time.

thanks,
Jon
 
M

mlimber

Jon said:
Ok, I'm going to go ahead and see what I can learn about traits and generics
for so I have some idea. Hopefully I find a good resource out there that can
give me a decent over view of them without taking to much time.

I'd recommend Stroustrup's _The C++ Programming Language_ (3rd edition)
for that task. It's not a book for beginners, but it is, IMHO, the best
guide for intermediate programmers. See these FAQs for more
suggestions:

http://www.parashift.com/c++-faq-lite/how-to-learn-cpp.html#faq-28.6
http://www.parashift.com/c++-faq-lite/how-to-learn-cpp.html#faq-28.7

Cheers! --M
 
J

Jon Slaughter

mlimber said:
I'd recommend Stroustrup's _The C++ Programming Language_ (3rd edition)
for that task. It's not a book for beginners, but it is, IMHO, the best
guide for intermediate programmers. See these FAQs for more
suggestions:

http://www.parashift.com/c++-faq-lite/how-to-learn-cpp.html#faq-28.6
http://www.parashift.com/c++-faq-lite/how-to-learn-cpp.html#faq-28.7

Cheers! --M

Well, I think I have/had a strong foundation in C. When I "learned" C++
about 10 years ago I just thought of it as C with classes. I stopped
programming much after than and never really learned all the other stuff.
When I learned about templates I just thought about it as parameterized
classes and I guess thats all it is but I am failing to recognize the power
it they have. I've been reading the modering C++ design and while most of
the basic concepts are pretty simple I having a hard time seeing the big
picture.. I guess it will just take a some time to sink in.

I'll check out that book though.

Thanks,
Jon
 
M

mlimber

Jon said:
Well, I think I have/had a strong foundation in C. When I "learned" C++
about 10 years ago I just thought of it as C with classes. I stopped
programming much after than and never really learned all the other stuff.
When I learned about templates I just thought about it as parameterized
classes and I guess thats all it is but I am failing to recognize the power
it they have. I've been reading the modering C++ design and while most of
the basic concepts are pretty simple I having a hard time seeing the big
picture.. I guess it will just take a some time to sink in.

Do you have experience in object-oriented design? _TC++PL_ and _MC++D_
will teach you *how* to do things in C++, but they don't concentrate on
*what* to do and *when* to do it. For the what and when, check out, for
instance, _Design Patterns: Elements of Reusable Object-Oriented
Software_ by Gamma et al. As I mentioned, _MC++D_ builds on _Design
Patterns_, and without understanding something about design patterns
and how they can be used, _MC++D_ won't be as useful.

Cheers! --M
 
J

Jon Slaughter

mlimber said:
Do you have experience in object-oriented design? _TC++PL_ and _MC++D_
will teach you *how* to do things in C++, but they don't concentrate on
*what* to do and *when* to do it. For the what and when, check out, for
instance, _Design Patterns: Elements of Reusable Object-Oriented
Software_ by Gamma et al. As I mentioned, _MC++D_ builds on _Design
Patterns_, and without understanding something about design patterns
and how they can be used, _MC++D_ won't be as useful.

Cheers! --M

Well, I think I have a general idea about design patterns except I just
don't have enough experience with it. I mean, I understand classes,
inheritence, multiple inheritence, etc... I think pretty well(atleast
decently) and I know for the most part how to use them to do things but I
guess its because I haven't done virtually any real programming in a very
long time that my brain just isn't able to see the big picture because its
forgotten to much of the details.

Like now my problem seems to be getting the semantics right for doing my
template stuff(see my other threads)... I'm just not understanding how the
template semantics work out. I can easily understand something like

template <class I>
class J
{

};

means.. if I do J<some type> then it just replaces all instances of I with
that type as if I had typed it in directly(atleast that is what I think...
maybe there is more).

But if I do something like

template <unsigned int I, unsigned int J, typename T>
struct Node
{
enum {i = I};
enum {j = J};
typedef T Class;
};

I understand I can do something like Node<1,4,some class> but I don't have a
clue what that "object" is. I "know" I can access the "fields" of that
"object" by using the scope such as Node<1,4,some class>::i should return 1.

It seems that the Node object just propagates a "meta container" through the
complication process and I can refer to it as a whole instead of having to
propagate the 3 pieces of data... so then I could do stuff like
Node<i,j,Node<a,b>> which propagates 4 pieces of information and through
recursion I could have it do as many as I want... all this seems to be
playing semantic games.

But I cannot understand for the life of me why something like

template <int i, typename T>
struct RT
{
};


template <int I, int J, typename T>
struct RT< Node<I,J,T>::i, Node<I,J,T> > : public Node<I,J,T>::Class
{
};

doesn't work. or even if I did

template <int I, int J, typename T>
struct RT< Node<I,J,T>::i, Node<I,J,T> > : public T::Class
{
};

It seems that the public part should be exactly the same(I guess it is
though).


I don't understand why, though, the

struct RT< Node<I,J,T>::i, ....

gives me some stupid error about it not being a simple type or something.

Isn't Node<I,J,T>::i just some literal value(an int)? and so shouldn't be
equivilent to just typing an int there?

Things like this I am confused on. I guess its cause I don't know the inner
works of templates and just know the very basics of them. But I can't seem
to find anything that discusses things like this.

Almost everything I find just show simple examples of how to do stupid stuff
with templates that does not talk about how templates actually work and what
the syntax and semantics are. i.e., the info I need so I can fix my
problem.

Anyways, I guess I just probably need to do some reading... probably the
strousoup or whatever book you mentioned. I'll try to get my hands on one
and see.

Thanks,

Jon
 
M

mlimber

Jon said:
Well, I think I have a general idea about design patterns except I just
don't have enough experience with it. I mean, I understand classes,
inheritence, multiple inheritence, etc... I think pretty well(atleast
decently) and I know for the most part how to use them to do things but I
guess its because I haven't done virtually any real programming in a very
long time that my brain just isn't able to see the big picture because its
forgotten to much of the details.

Are you talking about the mechanics of putting OO programs together, or
choosing what design to build in the first place. Design patterns are
higher level abstractions that make use of inheritance, virtual
functions, and the like.
Like now my problem seems to be getting the semantics right for doing my
template stuff(see my other threads)... I'm just not understanding how the
template semantics work out. I can easily understand something like

If it's the corner cases of templates that concern you, perhaps the
book you're looking for is not Stroustrup's, but _C++ Templates: The
Complete Guide_ by Vandevoorde and Josuttis. It covers templates in
great depth, I'm given to understand. (I haven't actually read it.)
There's also _C++ Template Metaprogramming : Concepts, Tools, and
Techniques from Boost and Beyond_ by David Abrahams and Aleksey
Gurtovoy, which I haven't read. I do use the results of their labors,
however, viz. the Boost libraries.

[snip]
But if I do something like

template <unsigned int I, unsigned int J, typename T>
struct Node
{
enum {i = I};
enum {j = J};
typedef T Class;
};

I understand I can do something like Node<1,4,some class> but I don't have a
clue what that "object" is. I "know" I can access the "fields" of that
"object" by using the scope such as Node<1,4,some class>::i should return 1.

It seems that the Node object just propagates a "meta container" through the
complication process and I can refer to it as a whole instead of having to
propagate the 3 pieces of data... so then I could do stuff like
Node<i,j,Node<a,b>> which propagates 4 pieces of information and through
recursion I could have it do as many as I want... all this seems to be
playing semantic games.

But such "semantic games" can be very useful and lead to clean code,
once all the templates are ironed out, and usually, that sort of thing
*is* relegated to a library like Loki or Boost where you and I can get
all the benefits of template trickery without struggling to get the
syntax right. See for instance the way _MC++D_ uses typelists in
chapter 9 or the way type traits (from Boost or Loki) can help in
"ordinary" template code.
But I cannot understand for the life of me why something like

template <int i, typename T>
struct RT
{
};


template <int I, int J, typename T>
struct RT< Node<I,J,T>::i, Node<I,J,T> > : public Node<I,J,T>::Class
{
};

doesn't work.

I think there's some arcane rule about a template parameter for a
partial specialization (e.g., your I, J, or T) being used explicitly in
the specializations parameter list. It doesn't count if it is only used
as a parameter to another template which is a parameter to the
specialization. So for instance if you did this instead, it would
compile:

template <int I, int J, typename T>
struct RT< I, Node<I,J,T> > : public Node<I,J,T>::Class
{
};

Perhaps some language lawyer out there can clarify, but I'm pretty sure
that's the problem.
or even if I did

template <int I, int J, typename T>
struct RT< Node<I,J,T>::i, Node<I,J,T> > : public T::Class
{
};

The same thing applies here, I think: RT<>'s specialization list needs
to use one of the template parameters I, J, or T explicitly.

[snip]

Cheers! --M
 
J

Jon Slaughter

mlimber said:
Are you talking about the mechanics of putting OO programs together, or
choosing what design to build in the first place. Design patterns are
higher level abstractions that make use of inheritance, virtual
functions, and the like.

Well, I tend to do ok on the design patterns I think... I can't hurt for me
to have some formal understand of them but I tend to be a "natural" at it in
the sense that if I understand the underlying principles of something I can
usually apply them well. Its not always the case ofcourse and it gets worse
every year ;)
If it's the corner cases of templates that concern you, perhaps the
book you're looking for is not Stroustrup's, but _C++ Templates: The
Complete Guide_ by Vandevoorde and Josuttis. It covers templates in
great depth, I'm given to understand. (I haven't actually read it.)
There's also _C++ Template Metaprogramming : Concepts, Tools, and
Techniques from Boost and Beyond_ by David Abrahams and Aleksey
Gurtovoy, which I haven't read. I do use the results of their labors,
however, viz. the Boost libraries.

Yeah, I need to understante templates more formally so I can use the more
advanced features of them without wondering if I'm doing it right. At this
point I'm kinda just throwing together things that seem to work but I'm not
completely sure if what I'm doing is really "Correct". Having a good
indepth guide would be nice because then I'd be more comfortable and not
stuggle so much with stupid things... Basicaly I just need to clear up some
of the formalities of templates. I'll check out those books.
[snip]
But if I do something like

template <unsigned int I, unsigned int J, typename T>
struct Node
{
enum {i = I};
enum {j = J};
typedef T Class;
};

I understand I can do something like Node<1,4,some class> but I don't
have a
clue what that "object" is. I "know" I can access the "fields" of that
"object" by using the scope such as Node<1,4,some class>::i should return
1.

It seems that the Node object just propagates a "meta container" through
the
complication process and I can refer to it as a whole instead of having
to
propagate the 3 pieces of data... so then I could do stuff like
Node<i,j,Node<a,b>> which propagates 4 pieces of information and through
recursion I could have it do as many as I want... all this seems to be
playing semantic games.

But such "semantic games" can be very useful and lead to clean code,
once all the templates are ironed out, and usually, that sort of thing
*is* relegated to a library like Loki or Boost where you and I can get
all the benefits of template trickery without struggling to get the
syntax right. See for instance the way _MC++D_ uses typelists in
chapter 9 or the way type traits (from Boost or Loki) can help in
"ordinary" template code.

Yeah, but the problem is that I don't understand the the technicalities of
those games. I do understand they can be used and sometimes have to be used
to do some pretty cool stuff, but its fuzzy on why they work and on exactly
how to use them.
I think there's some arcane rule about a template parameter for a
partial specialization (e.g., your I, J, or T) being used explicitly in
the specializations parameter list. It doesn't count if it is only used
as a parameter to another template which is a parameter to the
specialization. So for instance if you did this instead, it would
compile:

template <int I, int J, typename T>
struct RT< I, Node<I,J,T> > : public Node<I,J,T>::Class
{
};

Yeah, I just found that out from persenaama. I wasn't thinking well when I
asked that because its obvious now ;)
Perhaps some language lawyer out there can clarify, but I'm pretty sure
that's the problem.


The same thing applies here, I think: RT<>'s specialization list needs
to use one of the template parameters I, J, or T explicitly.

Ok, I think I see. I'm using I,J,T as parameters(but I'm not sure where they
come from... its just a little confusing but I think I just need to just
thinka bout it more) but in

struct RT< Node<I,J,T>::i, Node<I,J,T> > : public T::Class

we have
RT<int, class>

and the Node<I,J,T> returns a class for the second parameter which is ok,
the first Node<I,J,T>::i returns an enum or something like that... in any
case it just encapsulates I and there is no reason to use it... but I have
to use Node<I,J,T> to make sure I get a specialization of the Node type for
T.

I guess, still a little confusing ;/ These are the kinda things I need
cleared up... Maybe those books contain the keys. I will check them out
ASAP.

Thanks for your help,
Jon


[snip]

Cheers! --M
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top