|
-
November 1st, 2003, 09:40 PM
#7
Junior Member
Thanks for the kind words everyone, if you read through the tutorial you will notice that it just ends in the middle of a section. Well I got tired of writing and decided that there is enough information in this first part that I could just post it up. I'm actively working on it concurrently so you will get to see exactly how the internals of the std::vector templated class works.
slarty: I'm glad you learned something, if you wait and read my next section I guarentee you will learn considerbly more. Also thank you for fixing the code, I do compile all my code that I write but that doesn't prevent any logic errors from slipping by unnoticed.
chocalate
Your Tutorial is great ! Progme , thank you very much,,i hope you keep on the track to write the next of your tut like this.
May I know how you come to have this idea ? Could you explaine the most important thing to learn a programming lang ? i have problem with logical programming, lack of resources about the basic idea of programming..thanks in advance
Yes I will keep on track with writing these tutorials I do hope to some day publish all my writings into a book but we will see how it goes from here.
Hmm What do I consider the most important aspect in learning a programming language? Well it's obvious that you need to learn the syntax of a language so you are able to express your design to the computer but I woulnd't consider it terribly important. When I first look at a language I'll first look at the type system, is it strongly typed, weakly typed, latently typed, dynamically typed, statically typed? Generic programming is one of my more favorite parts of programming and if the type system gets in the way of it I'll probaly trash the language and move on. After that I'll look at the different paradigm's the language offers if it is like C and only offers procedural programming I'll probaly trash it as well. A good language will offer the most paradigm's, OOP, functional, generic, meta, etc.. That is when a language becomes the most intesting to me - exploring how they implement and extend paradigms.
Originally posted by WhiteEskimo
OH NO!!! My teacher just made us write this program in Java as an example of something NOT to do!!!! Slatry, do you understand why this program is so dangerous? What if n's value was 10000000?? Let me tell you what would happen. there would be portions of RAM set aside for each call to the function factorial() until n (which is equal to 100000000 or whatever) is equal to zero. Thats means that 100000000 positions of RAM would be filled up. Not only is this program extremely insuficiant, but it will also cause your computer to crash and other bad stuff to happen because you have filled up all your slots in RAM. Anyways, it looks like a simple program, but in fact if it isnt used properly, it could be deadly. My compiler will compile it when n=10 but if i set n=10000000000000 it wont even compile and it locks up and i got this error:
That would be true if the function was called recursively at run-time but the functions are instantiated(compiled on demand) when you compile the program. That's the thing about metaprogramming the compiler will generate it's own code and this must be done at compile time. The problem solves itself before the program is even ran. Now with any decent compiler there should be an option that will allow the compiler to generate unlimited amount of function depth, though it is usually kept off by default to prevent accidentle recursion.
Regardless this program was just used as an example I would hope any of my code in this tutorial would never be be slipped into a release build of a program.
Originally posted here by shred2er
I have a question. If the only templated parameter is known to be an integer, why would you even bother to write it as a templated function? Why not leave out the template specification altogether?
The cool thing about meta-programming is that it will solve the problem at compile-time rather then at execution time. This example will instantiate/generate all the code necassary to solve the problem. This is great because instead of computing the problem while the code is running it will already be solved before the program is executed.
You probaly will be thinking well I could just solve the factorial of 10 before the program starts and just hardcode the answer in and it would do the same thing. Well this is a truth and this example doesn't quite demonstrate the need for a metaprogram. More lucrative examples of meta-programming will be posted later on but to tie you over for now meta-programming can be used for rapid optimizations for complex functions.
It's kind of like the Cat example
class Cat
{
public:
void Meow() const;
private:
int age;
};
When a C-programmer see's this for the first time he is like "why have a class when I can just use a struct and call the function meow.
struct Cat
{
int age;
};
void Meow( Cat ) const;
The point is this program was shown to introduce the C programmer( a procedural programmer) to a new paradigm of Object Orientated Programming. Meta-Programming is a whole entire new exciting paradigm of code that codes itself! I would definatly suggest you reading over that section on meta-programming again it will hopefuly shed some light for you. If it doesn't I guess you will have to wait for my next tutorial that is a continuation on this to show some other cool examples of meta-programming.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|