News for hackers. Stuff that matters.
Re:A brief introduction to C++'s model for type- and resourc... by _yosefk on Friday January 15, 2016 @09:00AM (#10909302)
I don't deny that there are useful features in C++11/14 (though I find "modern C++" an irksome marketing trick by people peddling a disgusting programming language; it was called "modern" since at least the early 2000s and it was disgusting then.) Of course I'd rather iterate over std::map with a range for loop than use the C++98 way, and as to lambdas - while I find them way too awkward and limited compared to closures without the sharp edges (and with garbage collection), and a ton of "modern C++ code" went batshit insane in its extent of unnecessary reliance on lambdas, lambdas remain much more practical than the alternatives when doing things like some sort of a parallel_for. But then it's hard to find a language whose version X+1 adds nothing useful to version X.

Is C++14 a better language than C++98? It's hard to tell (I think it is - I think C++ is worse than C, but having gotten to C++98, perhaps it helps more than hurts to add all that other shit), but it's not an interesting question anyway because the upgrade is easy enough and it's worth it for most people for their specific reasons, so C++98 will die out.

But then it's not like Google aren't upgrading to the latest C++ standard, they're just doing it slowly AFAIK.

Re:A brief introduction to C++'s model for type- and resourc... by hellofunk on Friday January 15, 2016 @09:43AM (#10909553)
It's interesting to consistently find that people who hate C++ just really really hate it a lot, to the point of using profanity a lot of the time.

I don't see what the big deal is. C++ is probably the most gigantic and complex language I am likely to ever use... but you don't have to use the vast majority of it if you don't want to. If you want to stick with C-style coding, you pretty much can, if you don't want OO or STL or ++11/14 features -- nothing is forcing you to write those features into your code. But when you want access to them, there they are; and if suddenly you find yourself needing a particular library that only exists in C++ (as so many graphics and physics engines do), you can get access to that as well.

As soon as you need multithreading, or sorting of hash maps, or even basic string operations, knowing you can reach for them in pinch rather than find the C alternatives or build your own, seems like a big bonus to me (assuming a comparison to C).

Re:A brief introduction to C++'s model for type- and resourc... by 72deluxe on Friday January 15, 2016 @09:48AM (#10909580)
True, but I think a lot of the encouragement is to move away from writing C++ like it is C because we really don't want to encourage people to write with dangerous approaches anymore (like you can if you write C++ like it is C).

I work with a few who write their C++ like it is C (pointers thrown around, no const-correctness, memcpy instead of copy constructors etc. etc.) and it is horrible to read. There is no indication of lifetimes of objects, nor any indication of ownership. No RAII, no exceptions, nothing. It is dangerous.

You are rewarded for writing C++ safely because you won't have to fix any bugs and you can offload all the work onto the compiler for checking types and safety for you.

Like you, though, I don't understand people who get red in the face about C++ and go into a profane rage. It is just a language after all.

Re:A brief introduction to C++'s model for type- and resourc... by alfalfasprout on Friday January 15, 2016 @05:30PM (#10912737)
Here's the thing, there is a nice way (well, more than one) to blend highly tuned low-level C code with clean, modern C++. I have to do it: I work on high performance algorithmic trading systems.

You want to keep raw pointers, etc. confined to private class members, initialize memory only within the constructor (if possible), and free memory at the destructor. The user/public API should not expose unsafe stuff. With all this, RAII will give you a nice expectation of lifetime, etc. Plus you're able to use something like tcmalloc.

I often find a difficult issue arises when you have a large data structure that you want to free before it goes out of scope (to reclaim some memory). In this case, adding a "clear" method that frees the internals is handy. The destructor will call clear() but you can call it at any time. No harm done if it's called twice.

The next thing is const-correctness: if you follow it then any C-like code underneath should be relatively safe.

Not using copy constructors is just awful. Memcpy has a time and a place: at the low level (eg; I use it for serializing to a buffer). If you're using memcpy for classes you're probably doing something wrong.

Exceptions are a place where you see a wide gamut of safety. Some people don't use them at all: which isn't great. Others add hundreds of lines of code by wrapping everything in a try/catch. Not sure where I stand here, honestly. There's a point where you're catching exceptions that just aren't going to happen...

Re:A brief introduction to C++'s model for type- and resourc... by ratboy666 on Friday January 15, 2016 @11:44AM (#10910381)
Guilty. Of writing C++ as if it were C. Sometimes even with classes. Yes, I use memcpy instead of copy constructors. I find "modern C++" horrible to read. Indication of object lifetime? Ownership?

Um... why? If the development support does not include a garbage collector, I have to be very careful about these things. And, I am. Very careful indeed. I just use the approach I do in C, and assume NOTHING from C++. Honestly, I do NOT assume that the language system, compiler or run-time can do it properly. I have been bitten too many times.

I do presume a garbage collector at the very next level up.

Yes, you may find my code "horrible to read". And, yes, I have gone into profane rages over C++ code. It happens when the code bullies me -- with hopeless error messages for simple typos. When simple changes cause the application to no longer link. When simple changes cause massive explosions in object size. In fact C++ is so complex, it encourages cargo cult programming. Not just use of libraries, but enforced cargo-cult when USING the libraries. Because, raisons.

Not just a language -- hell-spawn. Sorry, "modern hell-spawn".

Re:A brief introduction to C++'s model for type- and resourc... by 72deluxe on Friday January 15, 2016 @11:50AM (#10910423)
The problem is that if you were hit by a bus, the next guy maintaining the code has to know about where everything is cleaned up and has to know all of the secrets that you know and that you decided you knew better than the compiler in areas. This makes for unmaintainable code.

Ownership is very important because if you don't know who owns it, who will clean it up??

If you don't know how long an object will be around because you've got a pointer, it is now a dangling pointer and attempts to use it will cause chaos. So object lifetime is important too.

The error messages are usually quite helpful. clang's messages are more helpful for gcc, I have found.

C++ is complex, but you can still write safe code without using any of the complexities.

Re:A brief introduction to C++'s model for type- and resourc... by vvanders on Friday January 15, 2016 @11:58AM (#10910487)
If you hate the idea of ownership and lifetimes I want to stay the hell away from any codebase you work on.
Re:A brief introduction to C++'s model for type- and resourc... by ratboy666 on Friday January 15, 2016 @02:40PM (#10911575)
No, what I "hate" is the idea that C++ promotes "automatic" management of "ownership and lifetime" without a garbage collector.

Which works, except when it doesn't. Which results in stuff like reference counting in the codebase. Which, of course, changes on a whim.

Example: the CEF3 library (chromium embedded framework).

Reference counted objects. Which requires source code changes depending on the version of CEF 3 being used. Requires VERY deep analysis to determine what to change in the using code. Really -- because the objects participate in a multi-threaded library.

So, most code ends up as "cargo cult".

Since my code doesn't fit into the CEF3 object model, I do the contortions needed -- building another layer.

Re:A brief introduction to C++'s model for type- and resourc... by vvanders on Friday January 15, 2016 @03:45PM (#10912008)
Ownership and Lifetimes != Reference counting.

Reference counting is code smell that you don't have a clear ownership model.

Take a look a Rust for something more along the lines of what I'm talking about. There's been quite a few people who've re-written their C/C++ code to Rust only to find out that they didn't have a clear ownership model and found architectural improvements because of it.

Re:A brief introduction to C++'s model for type- and resourc... by 72deluxe on Friday January 15, 2016 @03:58PM (#10912097)
RAII solves this. Allocate all you need in the constructor, tidy up in the destructor. So when the parent item gets cleaned up, the child items get cleaned up too (so lifetime is sorted for you, and ownership is done too as the parent that owns it is getting cleaned up). You will eventually only have one parent item but if you tidy everything up for that in its destructor, the lifetimes of everything is sorted.

If your acquisition of a resource fails or initialisation fails, throw an exception in the constructor.

C++11's threading support library in the STL should help your threaded problems.

If you need containers of items, put them into an STL container. Don't allocate them with new - write a move constructor for your item and it'll be moved. Or a copy constructor; follow the "rule of 5" if you have pointer-held items in your object (probably best not to), or "rule of 0" and let the compiler do it for you. std::move your items into the container.

Use const references if you want to let something else use it, ordinary references if you want them to modify it. Or if you must use naked pointers, put them in a unique_ptr so it gets cleaned up eventually (http://en.cppreference.com/w/cpp/memory/unique_ptr). Or shared_ptr in other areas (see Stroutrup 5.2.1 in C++ 4th edition).

If you need naked pointers, allocate them on the heap but wrap them in std::unique_ptr or have a vector of unique_ptr. If your objects are child items in a class hierarchy (e.g.. ChildClass : public BassClass) or the base class is an abstract class and you want to have a generic STL container (like std::vector<BaseClass* >) to store all base and child items in one neat place, you will have to use pointers (as you can't have std::vector<MyAbstractBaseClass> but you can have std::vector<MyAbstractBaseClass* >), you will need to take care in the containing object's destructor to clean up this container, eg. while(!vector.empty()) { delete vector.back(); vector.pop_back(); }

You shouldn't need reference counting. A read of Stroustrup's C++ Fourth Edition book and the "tour of C++" section toward the beginning will help you immensely.

Re:A brief introduction to C++'s model for type- and resourc... by scott_s on Friday January 15, 2016 @03:51PM (#10912060)
If you don't want to use basic C++ features such as copy constructors, then why use C++ and not C?
Re:A brief introduction to C++'s model for type- and resourc... by pcwalton on Friday January 15, 2016 @04:39PM (#10912313)
> You are rewarded for writing C++ safely because you won't have to fix any bugs and you can offload all the work onto the compiler for checking types and safety for you.

But that's not the case. No shipping version of C++ is memory safe. In fact, this is exactly what the article is about.

Re:A brief introduction to C++'s model for type- and resourc... by pjmlp on Friday January 15, 2016 @01:21PM (#10911050)
This is why although I like C++ a lot, nowadays my day job is spent between JVM and .NET languages, with C++ only when I need to step out of them.

From what I know, having the chance to ever find a job on my area where one can use proper C++ is very thin.

Re:A brief introduction to C++'s model for type- and resourc... by lmm on Friday January 15, 2016 @12:06PM (#10910544)
> C++ is probably the most gigantic and complex language I am likely to ever use... but you don't have to use the vast majority of it if you don't want to. If you want to stick with C-style coding, you pretty much can, if you don't want OO or STL or ++11/14 features -- nothing is forcing you to write those features into your code. But when you want access to them, there they are

The features are dependent on each other though. The flipside of my example elsewhere in the thread: if you want to use templates to eliminate repeated code, you more-or-less need to make any classes you want to template over use RAII (or else do very error-prone partial template specialization to ensure your things are initialized and deininitialized correctly). So then you have to use exceptions, because if you're doing complex logic in your constructors then you need a way to report failure. So then you have to make your code exception-safe, which means making everything else RAII.

Re:A brief introduction to C++'s model for type- and resourc... by hellofunk on Friday January 15, 2016 @01:31PM (#10911109)
Well, if you are going to choose just one feature to reach for in C++, template metaprogramming is probably not going to be it. Even a lot of devs who are very comfortable with other C++ tools and idioms don't write, if any, template code. I wouldn't think that someone would avoid all the other C++ features, but then willingly go down the road of writing templates.
Re:A brief introduction to C++'s model for type- and resourc... by agmcleod on Friday January 15, 2016 @11:37AM (#10910336)
I find this a lot more encouraging to read. Most gamedevs I see talking about C++ mention to avoid boost & templating, how STL is bad, etc. How they have to write their own more efficient version of vectorm or their own allocator. There are things I like things about C. I find malloc & free fairly understandable, where as a lot of the newer pointer arithmetic in C++11 can be a bit overwhelming.

That said as someone who hasnt done a lot of C or C++, i'd like to think learning the new way is best, otherwise it's hard to know where to start.

Re:A brief introduction to C++'s model for type- and resourc... by pjmlp on Friday January 15, 2016 @12:56PM (#10910869)
The thing with game devs is that they only change tooling when forced to do.

In the 80's Assembly was the only way to go, if you were targeting consoles and home systems.

Higher level languages were the managed languages of the day.

When the SDK started to push for C, like on first PS, they were forced to move. Still the code was pretty much inline Assembly wrapped in C, until they were confident to write more and more in C.

Similarly when around PS 3 time, the SDKs were moving into C++, the majority took advantage of the C compatibility and used only as much C++ as required by the SDKs.

Only recently some started to do the transition to embrace C++.

Likewise if in the future language X becomes the only way to target games for platform Y, and they a market that they need to be in, they will eventually adopt it. Of course writting big chunks in C++ until they can be convinced it is ok to just use X.

Re:A brief introduction to C++'s model for type- and resourc... by agmcleod on Saturday January 16, 2016 @06:47PM (#10917441)
Ah interesting. I have to admit I thought the C++ dominance had been around for longer than that.
Re:A brief introduction to C++'s model for type- and resourc... by sitkack on Friday January 15, 2016 @11:05AM (#10910093)
I tried being a C++ programmer back along time ago (1992-1995) and I failed. The biggest mistake was trying to learn the whole language and apply the whole language to the problem. The utility of the language doesn't compensate for the cognitive burden. I would have been better served by using ML (with some C), or Rexx (with some C). Good C++ has an abstract top level that isn't much different than a scripting language in expressivity. Now something like a Lua +Rust can be more expressive, correct and fast.
Re:A brief introduction to C++'s model for type- and resourc... by pjmlp on Friday January 15, 2016 @11:30AM (#10910275)
Although I would also favour ML over C++, if you look its derivatives, OCaml, Haskell and so on, they really aren't much smaller than C++.

C++ biggest problem are all the design decisions to fit into C's tooling and its compatibility.

Re:A brief introduction to C++'s model for type- and resourc... by GFK_of_xmaspast on Friday January 15, 2016 @02:49PM (#10911626)
Early 90s c++ was pretty unfortunate all around.
Re:A brief introduction to C++'s model for type- and resourc... by blub on Friday January 15, 2016 @11:00AM (#10910047)
He's not just any guy, but the guy that created a whole website, C++ FQA, dedicated to trying to explain how C++ is terrible. Or maybe he just shares the same name with him.

Anyway, it's difficult to have a reasonable discussion over a topic where said topic has religious dimensions for one of the involved parties.

Re:A brief introduction to C++'s model for type- and resourc... by _yosefk on Friday January 15, 2016 @03:49PM (#10912035)
That's me alright, but I think my comment in this instance didn't warrant the claim about "religious dimensions." Having a firm opinion and using (OMG) the word "shit" when stating it does not mean it's a religion, and I'm very secularly using much of C++11 as I said, and I was among the people pushing for the upgrade where I work.
Re:A brief introduction to C++'s model for type- and resourc... by blub on Wednesday January 20, 2016 @03:41PM (#10941170)
Maybe it was too strong of a word, but... I have the impression that C++ topics are more emotional than they need to be. In the end it's just a programming language.