Hacker News new | past | comments | ask | show | jobs | submit login
Modern C++: don't use f*cking pointers. (klmr.me)
14 points by Ideka on July 9, 2013 | hide | past | favorite | 7 comments



Generally this is reasonable advice although I would not have stated it quite so emphatically.

Most of the time when I type * in my own C++ code, what I mean is:

    "hey! this is an in/out parameter!".


Good thing that the author provided such an exhaustive list of arguments supporting his opinions. Oh wait. Between using pointers and the standard library, I think that I will stick with pointers.


To me at least build_new_object example seems horribly wrong: If you allocate the object on the stack and then returns it, it will go out of scope with the function, and the memory overwritten with next stack usage. Or does modern C++ do some magic nowadays?


No it will use a copy or move constructor to return the object by value, no problem there. Notice the value is being returned, not a pointer to the value.


Right, and on a good day you might get named return value optimization.

http://msdn.microsoft.com/en-us/library/ms364057(v=vs.80).as...

If the compiler can figure out what you're doing, the huge_object you declared can be aliased to an object in the caller's stack frame which results in no copies or moves at all.


Ah thanks for clearing that up. It's in a newer (C++03?) standard. I still have to deal with gcc 2.95 on a regular basis so I'm stuck in the old ways. I guess that is why the title was 'Modern C++'.


I don't think that it even can do anything about that. It will just go out of scope and that's that.




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

Search: