Let me correct myself and some disclaimers.
This is not a critique of STLs but how bad use can turn this into nightmare.
STL is one of best things to happen for C++.
It allows rapid creation of code (almost like interprted language). But there are some inherent problems here.
(Though I don't know how to overcome it).
For example take STL string. Default allocator moves to new but I can tweak it to customized allocator. But consider I want to code like below:
void fun()
{
char buf[128] = "test";
manipulate(buf,128);
}
Now manipuate is a library function which doesn't takes string. What to do? Well I think the usual answer is library design is wrong? Change the library to accept string like below.
Well this is disater. The code that was mainpualting data structures on stack has suddenty moved on heap.
void mainputae(std::string & str);
Now try to change default allocator. Will it work.
Something like below:
void fun()
{
std::string<....,my_alloctor> str;
manipuate(*((std::string *) (&str)));
}
This also doesn't work. Since the allocator function is like below:
void * p = allocator::allocate(size);
This code is inside library. Inside the libtary the default allocator is still default so no use.
Actually STL are not meant for interfaces. Interfaces are designed so as to hide implementaion details and STL philosphy is not for that.
STL aim to provide generic algorithm and express is as type independent and key decisisions can be tuned as per details (memory allocation etc.) But tuning it to different types can lead to different data types. Hence the type is not completely defined.
Don't use STL for interfaces. (library design and you want to give library with simple .h types).
More about this in next post.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment