Archived

This topic is now archived and is closed to further replies.

neurokaotix

Are templates, namespaces and std:: worth using?

Recommended Posts

I was checking out the OGRE engine source and I noticed that they seem very hung up on using namespaces, templates, and that std:: thing (btw, I'm not familiar with what these things actually do). My engine does a lot of the things that OGRE does (at the structure level, not level of completeness as an engine). Do they make the engine run slower? I've heard a bad things about all of the above mentioned methods. Are these things really necessary? Join the World Wide Revolution: [edited by - neurokaotix on March 2, 2003 7:55:52 PM]

Share this post


Link to post
Share on other sites
they aren''t neccessary, but they can certainly help you. I make heavy use of the STL and containers. At first they look a bit messy but you soon get used to them. As for them making your application run slower, that''s possibly true, but I also know that they are fairly well optimised so they shouldn''t noticebly slow you down. Besides, if you write your own containers and data structures, are they likely to be quicker than ones written by professionals ?

Share this post


Link to post
Share on other sites
Templates usually help making things work with less code. Maybe even faster than without them, if without them one wouldn''t bother writing so much as is necessary (eg. you''d be content with sub-optimal solutions since you wouldn''t want to write 1000 lines of code again and again). The bad thing with templates is that some older compilers don''t support them or support them poorly (I''ve heard nasty rumours about VC6).

Namespaces don''t affect runtime speed at all. They help organize code.

std:: means that OGRE uses the standard C++ libraries (not deprecated old stuff), and they lie in std namespace. Again, using the std namespace does not affect speed at all.
quote:
I''ve heard a bad things about all of the above mentioned methods.
What exactly could it be?

Share this post


Link to post
Share on other sites
Are they necessary? No, but they certainly help.

The standard C++ library (the std:: thing) *could* make your engine run slower if used inappropriately.

What bad things have you heard? They usually come highly recommended (when their use is called for).


[edited by - Solo on March 2, 2003 8:04:15 PM]

Share this post


Link to post
Share on other sites
as for namespaces, I''ve heard that they get really confusing or something, for templates that they slow things down a lot, and for the std thing nothing, because I didnt even know what it is

Share this post


Link to post
Share on other sites
Templates don''t slow things down at all, there is no unecessary decisions to be made at runtime because the compiler will re-generate the templated code with each different type that is sent to it. In other words, and my computer science professors drill this, templated code IS NOT CODE. The compiler generates the code based on the template and the data type sent to it. This will not increase execution time but will increase compile time and executable size.

As for namespaces, they are basically organized global variables.. no slowdown there.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
As the other posters already said, these things are not *necessary*.

But I highly recommend to use them or to consider to use them when it''s appropriate. There are some benefits and some tradeoffs.

Namespaces are to package your software into ''modules''. Not on a file basis but on a ''inter-file'' basis. Relating classes and functions can be put into one namespace so they are packed together. So, namespaces help to structure your software. They where introduced in C++ to make it easy for compiler writers to implement them. And damn, they are by far not complete/sophisticated as the package system of Java for example. You have to be quite careful (sometimes) and they suck (quite often) when you import same names from different namespaces. But nevertheless, they are really better than prefixing every class with the programs/frameworks name. Using them will often result in a better structured program. So I would recommend to use them.

STL: The Standard Template Library was developed to provide (type)safe containers and other classes. A big emphasis lies on performance. Selfwritten containers/code will always be slower if they are not very specialised. Believe me. Almost no chance to get them faster for general tasks. Use them

Templates: These support generic programming. Templates are there to parameterize classes and functions/methods. The STL makes heavy use of them. The standard example is the vector class. You pass another class to it when creating an instance:

vector<Item> v;

This creates a Vector of Item items. It takes time to completely understand templates and when to use them, but it is one of the best things in C++. They give great support. If they wheren''t there you would have to cast around when using containers, e.g (see Java). Parameterized classes can provide a uniform interface for different implementations. Ogre uses them for choosing between rendering backends I think. Learn, make some experience and then use them in your software. That''s all

All these thing are one the good things C++ have. They can be misused (that''s bad), as almost all in C++. But not using them, means that you don''t really use the ++ in C++.

And as a final note: Performance is a matter of design, very much more than a matter of coding.

Share this post


Link to post
Share on other sites
Namespaces are meant to avoid name collisions, templates are the C++ mechanism for generic programming. Both have no run-time cost whatsoever, they are a compile-time only mechanism.

"That std:: thing" is an example of explicit scoping. Just like you need to refer to static members as MyClass::MyMember, you need to refer to symbols within namespaces as MyNamespace::MySymbol. Adding a using MyNamespace::MySymbol; aliases MySymbol to MyNamespace::MySymbol, saving you some typing. using namespace MyNamespace; does the same thing for all symbols in the namespace.

Most people diss them (and the standard library) mostly out of ignorance and hearsay, are to lazy to learn to use them (Hey, C++ is just C with classes, right ?), not to mention to use them properly or even figure out what exactly the C++ standard library provides ( in your own words "That std:: thing" ... ).

Keep in mind that you rarely miss what you don''t know about.

Do you *need* them ? No, but then again, neither do you need classes. Or functions. Functions execute a same bit of code with different parameter values. Templates generate classes and functions with different parameter types.

Instead of using a function, you could rewrite the function code every time, directly plugging in the parameter values. Instead of using a template, you could rewrite the class or function everytime, just changing some types (e.g. a ListOfModels instead of a ListOfTextures). My opinion is that it is better to let the compiler rewrite those bits for you (std::list<Model> and std::list<Texture>


Now, as to whether you need to use the C++ library... no.
But neither do you need to use the C library, or the OGRE engine.
You could rewrite them all yourself, but would you want to ?
Look at how many people are unable to write a correct linked-list class... not to mention know when it is appropriate or not.

Both the C and C++ libraries are designed to work well in most cases. Thus a special implementation may outperform them in those special cases it targets. But of course it will take some work to provide the same functionality (especially since C++ library components are meant to interact with each other), and some attention to detail to make it work correctly and efficiently.

Probably more work than it takes to get working knowledge of the standard library. Plus, even if you end up writing your own classes, you usually benefit much from making it compatible with standard libraries : don''t your own functions assumes C strings are null-terminated so that they can work nicely with C library functions ? Well, it''s the same with the C++ library. Even more so with generic programming.


Anyway, learning about all that stuff certainly can''t hurt you, can it ?


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
quote:
as for namespaces, I've heard that they get really confusing or something, for templates that they slow things down a lot, and for the std thing nothing, because I didnt even know what it is
You've heard wrong.

Templates work at compile time. They may make your compile times longer. They can be used in so many different ways it is amazing. They can produce very efficient code. In the old world of computing the places that first used computers large scale were telecommunication companies, banks, science research, the stock market, the military. Many of the tasks were just number crunching (it wasn't to do with slick desktop applications or games). Solving linear equations was (and still is) a big deal. Inverting matrices, multiplying, adding etc. These routines were written in FORTRAN and have been optomised for 30-40 years. They are very good and very quick. Some people have written a matrix library in C++ using templates and have achieved similar performance. The library is well designed and is extensible, adaptable, very generic to different data types and containers. This is what templates can buy you if used well.

Namespaces make things clearer. std:: is the namespace used by the c++ standard library. It keeps all names like list and vector neat and tidy. You can't refer to them by accident. There are sure to be lots of other classes or functions in the world called list. Namespaces mean you can consistently name things instead of just having neuroList, neuroVector, neuroWindow etc. What this means is you have a common prefix neuro::list, neuro::vector, neuro::window. You may think this doesn't buy you much but consider this - if you've written some code to use your list and then decide to change to use std::list instead you can just put using std::list; instead of using neuro::list; . You can make this simpler by using typedefs. e.g.

You can just comment out the one you don't want to use and do some tests.

        
typedef std::list<bullet> bullets;
//typedef neuro::list<bullet> bullets;


bullets playerAmmo(20);
bullets enemyAmmo(50);
bullets levelAmmo(75);


[edited by - petewood on March 3, 2003 6:18:54 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Do expression templates require something like a meta copiler? If yes, then they are bad. Instead, I recommend the boost libraries. They are really great. It''s worth to take a look.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Do expression templates require something like a meta copiler?

Why don''t you try reading the article?
quote:

If yes, then they are bad.

How so?

Share this post


Link to post
Share on other sites
No meta compiler required, just a highly conformant C++ compiler.

C++ templates definatly do not slow things down. In fact, I can''t think of a way that they can slow things down, because they work at compile time only. Generic Programming, Compile Time Polymorphism, that kind of stuff.

let''s take an example:

Say I have a program that needs to calculate..say...n factorial a whole lot of times in my program. Doing this in run time is expensive, but say for instance I know these numbers at compile time, n.

I can write a template to do it for me.



template < unsigned int n>
class Factorial {

enum { value = Factorial< n-1 >::value*n };

};

template < >
class Factorial< 0 > {
enum { value = 1 };
};



Could also use a static const int64 or something so you don''t overflow the enum, but that''s implementation.



Gamedev for learning.
libGDN for putting it all together.
An opensource, cross platform, cross API game development library.

Share this post


Link to post
Share on other sites
Visual Studio.NET 2K3 supports them as well as any other compiler I''ve seen. VC6 is the worst, but that came our before the standard was finalized.



Gamedev for learning.
libGDN for putting it all together.
An opensource, cross platform, cross API game development library.

Share this post


Link to post
Share on other sites
quote:
Original post by CpMan
VC6 is the worst, but that came our before the standard was finalized.


I''m pretty sure Visual C++ 6 was released after the standard was finalized, but only by a month or two, so the compiler code were probably pretty much done when the standard was approved. While that would make it unreasonable to expect 100% standard conformance, the support could certainly have been vastly better if they had put any effort into it, with Microsoft involved in the standardization process and all...

Share this post


Link to post
Share on other sites