Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Having no experience with C++ the next statement will just further show my ignorance of the language.

I was under the impression that an optimizing C++ compiler would be able to inline the container object and the contained object into one when working with template code, so that you would end up with something exactly like the C version but without the manual bookeeping.

At least I thought it could do this for certain types of classes, much like an optimizing C compiler can selectively inline functions based on heuristics.



It's not using heuristics. It's how the code is actually written.

An implementation of std::list<T> is going to contain multiple objects of type __node<T>.

__node<T> would be defined as:

    template <typename T> struct __node {
        __node *prev;
        __node *next;
        T value;
    };
(Note that this doesn't change what anyone else said though, T is a pointer in this particular case, so there's still another indirection)


You are on the right track here, but consider what the contained object is:

    std::list <person*> people;
You're right that the instantiated list entry (what the article calls "helper") will directly include a value, but the value here is a pointer to a person, not a person.


There is nothing stopping him from having a list of person and avoiding the memory overhead of references though.


I had overlooked that he was using a pointer to the struct, and so I was interpreting things to be that C++ was actually doing under the hood allocations. I'm glad I asked though, cause I was really worried about C++ for a second there :)

So it seems like if the author had wanted he could have used C++ like a safer version of C macros, which if I ever need to use C++ will likely be my approach too.


It wouldn't even require an optimizing compiler just one that properly implemented templates. As megrimlock points out that would be a list of person not a list of pointers to person. Also his delete performance issue would not be an issue if done correctly. It would be a one liner of person_list.erase(person_iter).

So far as I can tell he is either trolling or has internalized procedural programming so much that he can't see any other path. I wonder what his thoughts on FP are.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: