本文发表在 rolia.net 枫下论坛I understand your point and respect that.
However I insist that vector is supposed to be a generic container, therefore insert() method should also be able to handle any type, not only primitive tpes like int, char. And as we've seen, it truly takes more than a couple of lines to implement a vector class.
I'm not saying that you can't write your own stuff, especially something of special requirement and not available yet in library. However, if you ask me, I wouldn't write my own version of common containers like stack, list or vector, because of two reasons
1) Most importantly, I want my code easier to read/maintain for others
2) I'm not confident that my containers will be better than STL :-), especially after spending some time reading exceptional/more exceptional C++ and STL source code, then realizing so many things I don't know, and sweating a lot
On a side note, I don't think it's correct to say when you use STL, the binary size grows bigger mostly because of the linked libraries. After all, STL or C++ template code, are just a bunch of header files ( even they can be named .c, .cc sometimes), nothing more than smart macro. Before you instantiate them, they only contain pseudo code, only when you instantiate it, the compiler generates real code. IMO template inflation caused by instantiation is mainly responsible for this, not linked library.
However, this is almost unavoidable due to the nature of C++ template. Even if somebody writes his own template version with the same functionalities of STL, I doubt it can do much better than STL. Actually the STL source code is quite smart to me when it comes to reducing template inflation. It exacts the non-generic , i.e. variable/method of fixed type/signature, and create non-template base class to encapsulate them, then the generic class template inherits from them, thus reduce the code size inflation.更多精彩文章及讨论,请光临枫下论坛 rolia.net
However I insist that vector is supposed to be a generic container, therefore insert() method should also be able to handle any type, not only primitive tpes like int, char. And as we've seen, it truly takes more than a couple of lines to implement a vector class.
I'm not saying that you can't write your own stuff, especially something of special requirement and not available yet in library. However, if you ask me, I wouldn't write my own version of common containers like stack, list or vector, because of two reasons
1) Most importantly, I want my code easier to read/maintain for others
2) I'm not confident that my containers will be better than STL :-), especially after spending some time reading exceptional/more exceptional C++ and STL source code, then realizing so many things I don't know, and sweating a lot
On a side note, I don't think it's correct to say when you use STL, the binary size grows bigger mostly because of the linked libraries. After all, STL or C++ template code, are just a bunch of header files ( even they can be named .c, .cc sometimes), nothing more than smart macro. Before you instantiate them, they only contain pseudo code, only when you instantiate it, the compiler generates real code. IMO template inflation caused by instantiation is mainly responsible for this, not linked library.
However, this is almost unavoidable due to the nature of C++ template. Even if somebody writes his own template version with the same functionalities of STL, I doubt it can do much better than STL. Actually the STL source code is quite smart to me when it comes to reducing template inflation. It exacts the non-generic , i.e. variable/method of fixed type/signature, and create non-template base class to encapsulate them, then the generic class template inherits from them, thus reduce the code size inflation.更多精彩文章及讨论,请光临枫下论坛 rolia.net