News for hackers. Stuff that matters.
Re:A brief introduction to C++'s model for type- and resourc... by hellofunk on Friday January 15, 2016 @07:31AM (#10908868)
What Modern C++ practices specifically have you found that Google discourages? I'm curious. I'm reading through Scott Meyers' Effective Modern C++ and am finding so much easy and fantastic advice, I wonder if Google is at odds with some it?
Re:A brief introduction to C++'s model for type- and resourc... by otabdeveloper1 on Friday January 15, 2016 @07:38AM (#10908905)
> I wonder if Google is at odds with some it?

It's at odds with almost all of it. Google's standards are C-with-classes based on compiler bugs circa 1999.

Despite their (misleading) marketing, Google is still just another enterprisey megacorp employing 30000 blub programmers. Expect the usual 'enterprise' failures.

Re:A brief introduction to C++'s model for type- and resourc... by petke on Friday January 15, 2016 @09:06AM (#10909327)
When I read the Google coding standard I realized I never want to work there. Microsoft on the other hand seems to get cpp. Maybe because herb Sutter has managed to change the culture from the inside. Anyone remember how awful vc6 was back in the day? MS has come a long way.

Edit: It seems the standards have changed. It doesn't seem so bad now. They cant use exceptions but they can use some of boost cpp11 and templates. So maybe working at google ain't that bad.

Re:A brief introduction to C++'s model for type- and resourc... by _yosefk on Friday January 15, 2016 @07:55AM (#10908975)
> Google is still just another enterprisey megacorp employing 30000 blub programmers. Expect the usual 'enterprise' failures.

Ouch! That must have hurt 'em. Of course the original meaning of "Blub" is "anything which isn't Lisp", especially C++ (as well as say Java.) But what the heck, it has a good sound to it.

Anyway, I think their C++ style guide isn't bad. As to "modern C++ style", let's wait for a few years, shall we? When the STL came out, auto_ptr and for_each(v.begin(), v.end(), FunctorFromHell()) were marketed as the way to go for a long while; then the usual suspects became a bit more silent. Now again, despite their being a smart for loop, for_each with a lambda (with its crazy capture lists and all) is supposedly considered at least as good as a for loop, and typically better. And you're supposed to use unique_ptr and what-not. Maybe a few years down the road things will look different again.

C++11/14 has a ton of weird shit you ought to keep in your mind on top of the weird shit in C++98/03 and many "enterprisey megacorps" seem to apply a restricted subset of C++ rather successfully.

Re:A brief introduction to C++'s model for type- and resourc... by otabdeveloper1 on Friday January 15, 2016 @10:21AM (#10909767)
> many "enterprisey megacorps" seem to apply a restricted subset of C++ rather successfully

Your definition of 'successfully' is vastly different from the Megacorp definition of 'successfully'. I interview C++ programmers _all the time_, and let me tell you: practically the only reason those restricted subsets of C++ exist is to allow bad programmers to work at Megacorp.

There are a lot of really unqualified C++ programmers in the world, and by far the biggest problem faced by Megacorp is employee churn, not code quality and not product deadlines.

The use of 'C++ subsets' solves the employee churn problem at the cost of code quality.

Re:A brief introduction to C++'s model for type- and resourc... by p0nce on Friday January 15, 2016 @10:51AM (#10909980)
Agreed. If you are using C++ but no exceptions, no RAII, and no std::unique_ptr, you are avoiding the best part of C++.
Re:A brief introduction to C++'s model for type- and resourc... by vvanders on Friday January 15, 2016 @11:55AM (#10910460)
I'm with you on RAII and unique_ptr but I've yet to find exceptions to be compelling enough. I find Rust's approach(Result<T,E>) much nicer.
Re:A brief introduction to C++'s model for type- and resourc... by stinos on Friday January 15, 2016 @02:54PM (#10911663)
I've been looking for something similar for C++ for a while now, after getting fed up with yet another prototype like

    bool OpenFile( const std::string& path, File& f, std::string& err )
or similar, you get the point. At the moment I'm using a class like llvm's ErroOr<T> [1] and I'm loving it so far. The aformentioned function would be written as

    ErrorOr<File> OpenFile( const std::string& path )
and contain an std::error_code if opening failed, or else a File instance.

[1] http://www.llvm.org/docs/doxygen/html/classllvm_1_1ErrorOr.h...

Re:A brief introduction to C++'s model for type- and resourc... by lorenzhs on Saturday January 16, 2016 @07:47AM (#10915134)
That's a very useful pattern, functional languages often call it the maybe monad. I wish more people used it in other languages. This is my version of it: https://github.com/lorenzhs/algen-framework/blob/master/comm...
Re:A brief introduction to C++'s model for type- and resourc... by stinos on Saturday January 16, 2016 @09:14AM (#10915297)
That's actually std::optional, minus the nothing/just notation? Indeed more people should use things like this. Reminds me of a similar concept I also have wrappers for: instead of manually having to compare the results of std::find etc against range.end() all over the place, the result is basically in a small container which has both the iterator returned and the end of the range. So I write

    const auto result = alg::find( range, pred );
    if( result )
      doStuff( *result ); //*result is reference to found element
instead of typcial

    const auto end = range.cend();
    const auto result = std::find( range.cbegin(), end, pred );
    if( result != end )
      doStuff( *result );
Not that much of a gain, but once used to it I could never go back. And I realize now this could actually just be rewritten using your maybe class or std::optional.
Re:A brief introduction to C++'s model for type- and resourc... by lorenzhs on Saturday January 16, 2016 @09:37AM (#10915348)
Yeah it's pretty much std::optional but unfortunately that's in the library fundamentals TS and still in the experimental namespace last I checked. But I have to say that I prefer the name "maybe", it does convey the notion that you have to check whether it's valid much better than "optional" in my opinion.
Re:A brief introduction to C++'s model for type- and resourc... by vvanders on Friday January 15, 2016 @03:46PM (#10912016)
Yup, this is exactly like Rust's Result<T,E>(with E being std::error_code).
Re:A brief introduction to C++'s model for type- and resourc... by lmm on Friday January 15, 2016 @12:01PM (#10910506)
The functional approach is great in a language with typeclasses or modules. But it's not a good fit for RAII - how would a C++ constructor report an error using Result?
Re:A brief introduction to C++'s model for type- and resourc... by vvanders on Friday January 15, 2016 @12:07PM (#10910550)
By not doing any work in the constructor.

IMO constructors should be setting sane defaults and nothing more. If you need to do work that could fail put that into an "init()" function with a return type that can signal an error.

I worked for quite a while on platforms that didn't have exceptions and this was the standard pattern for keeping RAII without needing them. If you wrap the return value correctly(like with Result<T,E>) you can also guarantee that your clients need to deal with the error case(or fail to handle it with unwrap()) as opposed to getting caught by an exception that wasn't declared or documented.

Re:A brief introduction to C++'s model for type- and resourc... by lmm on Friday January 15, 2016 @12:24PM (#10910674)
> IMO constructors should be setting sane defaults and nothing more. If you need to do work that could fail put that into an "init()" function with a return type that can signal an error.

That allows you to construct the object in an invalid state. The whole point of RAII is to avoid that. The acronym literally means not having a separate init() function.

Re:A brief introduction to C++'s model for type- and resourc... by vvanders on Friday January 15, 2016 @01:00PM (#10910896)
Sorry, I should have clarified that the init function is usually static so you can invoke with it returning an object + possible error code if you never want an object in an invalid state.

FWIW some quick googling shows that having a constructor acquire a resource is not a requirement(https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Resource_A...). The important part about RAII is that the automatic releasing of resources when out of scope(through a no-throw destructor).

Re:A brief introduction to C++'s model for type- and resourc... by Manishearth on Friday January 15, 2016 @02:32PM (#10911521)
You can solve this problem the Rust way too: don't use a constructor. Create a static `new` method that returns the object to you, with optional error flags.

This is still RAII, you're just not using a constructor to acquire. Rust doesn't have constructors and is still RAII.

Re:A brief introduction to C++'s model for type- and resourc... by pjmlp on Friday January 15, 2016 @01:18PM (#10911029)
While I also don't like the exception handlers in constructors syntax, not putting any work in the constructor doesn't mean that data members or base classes cannot fail to initialize.
Re:A brief introduction to C++'s model for type- and resourc... by steveklabnik on Friday January 15, 2016 @12:59PM (#10910890)
In Rust, we don't have special constructors, just functions, and to construct a struct, all of its members must be valid, and assigned at the same time. So a Rust-style "constructor" in this vein would return a Result<MyStruct, Error>, basically.
Re:A brief introduction to C++'s model for type- and resourc... by p0nce on Friday January 15, 2016 @06:51PM (#10913235)
We are seing lots of .unwrap() because Rust has no monadic notation. That replaces recoverable errors with crash.
Re:A brief introduction to C++'s model for type- and resourc... by kibwen on Saturday January 16, 2016 @04:10AM (#10914801)
I don't think monadic notation here is the panacea that it is sold as. The harder part would still be modifying your function's return type to thread the result up the stack.

In any case, .unwrap() is perfectly fine in applications, it's only in libraries that they're discouraged.

Re:A brief introduction to C++'s model for type- and resourc... by _yosefk on Friday January 15, 2016 @02:56PM (#10911682)
Well, your definition of bad C++ devs may differ from mine or Google's. Or it might not, but for all I know, you might require knowledge of arcana that I don't care about, even when I happen to know it.

To me success includes being able to find (not frequently replace - I like working with the same people for many years - but find new ones in order to grow) developers that can contribute to the code base, and some of the best ones I know can't be bothered to learn much of C++. Go figure. Maybe you and I legitimately disagree because we work on different things, and maybe one of us is wrong.

Re:A brief introduction to C++'s model for type- and resourc... by hellofunk on Friday January 15, 2016 @07:59AM (#10909000)
It's hard to deny that there are some really useful new features in modern c++, though. Standardized multithreading primitives is a big one. Smaller ones like scoped enums are useful too. I'm a big fan of lambdas and the ability to use them for things that used to be very awkward to do without them, like std::bind and functors. In all, I think the modern additions are a net positive for sure.
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.
Re:A brief introduction to C++'s model for type- and resourc... by 72deluxe on Friday January 15, 2016 @08:54AM (#10909271)
I would agree with this. I am writing C++03 at work due to lame compilers I am forced to use on Windows, and I really miss little things like std::atomic.

Lambdas would mean I could bin all those structs with bool operator()() for find_if that I have to litter my code with.

Re:A brief introduction to C++'s model for type- and resourc... by scott_s on Friday January 15, 2016 @09:55AM (#10909629)
I know a lot of people say "Boost is awful", but Boost provides the same atomic interfaces: http://www.boost.org/doc/libs/1_60_0/doc/html/atomic.html. Quoting from the introduction:

"Boost.Atomic is a library that provides atomic data types and operations on these data types, as well as memory ordering constraints required for coordinating multiple threads through atomic variables. It implements the interface as defined by the C++11 standard, but makes this feature available for platforms lacking system/compiler support for this particular C++11 feature."

Re:A brief introduction to C++'s model for type- and resourc... by 72deluxe on Friday January 15, 2016 @11:54AM (#10910457)
Thanks for the suggestion, much appreciated.
Re:A brief introduction to C++'s model for type- and resourc... by to3m on Friday January 15, 2016 @08:52AM (#10909259)
If you're of this opinion, you should be glad when people don't use them. This can be your competitive advantage.
Re:A brief introduction to C++'s model for type- and resourc... by hellofunk on Friday January 15, 2016 @07:46AM (#10908938)
>based on compiler bugs circa 1999

What do you mean by this?

Re:A brief introduction to C++'s model for type- and resourc... by pjmlp on Friday January 15, 2016 @07:44AM (#10908928)
- use of pointers instead of references

- C style error codes

- minimal use of STL

- use of templates is frowned upon

- C style enums

This just of the top of my head, I would need to read it again to see if they changed anything.

Re:A brief introduction to C++'s model for type- and resourc... by hellofunk on Friday January 15, 2016 @07:48AM (#10908944)
>minimal use of STL

I was recently reading the docs for a physics engine in c++ and they said that its initial implementation used std::vector in many places, but they instead implemented their own in later versions to accommodate maximum "portability and compatibility." I wonder what that means? Since the STL is included in C++ compilers and vector is always going to be there, why would they need to roll their own to make the engine either more portable or more compatible?

>use of templates is frowned upon

I wonder if this means the writing of templates, or using existing templates. If the latter, maybe like the STL their primary concern is compile times?

Re:A brief introduction to C++'s model for type- and resourc... by sgeisenh on Friday January 15, 2016 @09:11AM (#10909360)
Use of templates isn't frowned upon. TMP is frowned upon because usually the performance gains don't justify the loss of readability. It has been said many times in many ways: don't write code for yourself, write it for others.

Basically, Google has an enormous code base. Stylistic decisions should have semantic weight, but since everybody has their own style, it becomes challenging to understand the reasoning behind certain choices. When you have a standard, it means that you can read into some of the stylistic decisions sprinkled throughout the code.

Take for example, the use of pointer arguments for passing the addresses of results. The justification is that using a pointer is explicit and unambiguous. Non-const references have value syntax but pointer semantics, which can be confusing. Yes, this is contrary to some of the core values behind C++ but Google felt that the improved readability was worth that dissonance.

Re:A brief introduction to C++'s model for type- and resourc... by 72deluxe on Friday January 15, 2016 @09:28AM (#10909467)
I would have thought that non-const references was better? It is clear that a non-const reference can be modified. If I just wanted to let them read a value, a const reference would be clear.

Do Google enforce const pointers for arguments that define where to store results? If not, you could delete the pointer they pass you. A pointer does not always indicate who owns the data clearly.

Far better would be to return the values from the function (or std::move them out if you must) instead of using a parameter as a "dump your results here" flag.

Stroustrup encourages this (see 5.3.3 of C++ 4th edition where he says "I don’t consider returning results through arguments particularly elegant"). IMHO, it would be better to return a std::pair<bool, std::vector<myWonderfulType>> where bool indicates the success/failure of the function, with pair.second item being the results.

Re:A brief introduction to C++'s model for type- and resourc... by zarkov99 on Friday January 15, 2016 @12:28PM (#10910704)
Pointers have the advantage of signaling a return value at the call site without having to look up the function prototype.
Re:A brief introduction to C++'s model for type- and resourc... by pjmlp on Friday January 15, 2016 @01:35PM (#10911128)
Which I don't agree with, given the amount of pointers that fly around in C codebases.

You cannot ever known what a pointer is used for.

If the prototype has const, maybe just maybe, assuming the function doesn't cast it away, it is a read only.

So I never bought into this argument.

Re:A brief introduction to C++'s model for type- and resourc... by plq on Friday January 15, 2016 @08:03AM (#10909022)
std::vector spec leaves quite a bit of performance-critical details unspecified. I imagine the allocator behaviour differs enough between implementations that they implemented their own to keep performance in different platforms more predictable. The performance requirements for a physics engine can be quite different from the general case that the std::vector spec is aiming for.

I imagine templates are frowned upon for the same reason -- too many moving parts. Otherwise they're perfectly fine to use in projects that have goals other than portably predictable performance.

Re:A brief introduction to C++'s model for type- and resourc... by hellofunk on Friday January 15, 2016 @09:38AM (#10909524)
Good points, I hadn't considered that the implementation of a vector could widely vary. That makes sense.
Re:A brief introduction to C++'s model for type- and resourc... by vvanders on Friday January 15, 2016 @12:02PM (#10910515)
Yup, the spec is also really vague on if clear() actually releases memory. AFAIK the only "correct" way to actually clear a vector is "vec = std::vector<T>()" with the STL.
Re:A brief introduction to C++'s model for type- and resourc... by pragmatib on Friday January 15, 2016 @02:41PM (#10911580)
Actually you'd have to try harder: even this might leave the current capacity unchanged. The blessed idiom is vec.swap(std::vector<T>()); c++11 added shrink_to_fit which is a non binding request to reduce capacity.
Re:A brief introduction to C++'s model for type- and resourc... by hellofunk on Friday January 15, 2016 @01:39PM (#10911159)
That is rather interesting!
Re:A brief introduction to C++'s model for type- and resourc... by programmer_man on Friday January 15, 2016 @10:28AM (#10909814)
It probably means that it was written before we had a complete and correct implementation of the STL on all major compilers.

Google around for Visual Studio 6 STL, and take into account the fact that most organizations don't upgrade right away, and you're looking at at least 2005 before you could rely on the STL cross-platform. Maybe much later.

Re:A brief introduction to C++'s model for type- and resourc... by lmm on Friday January 15, 2016 @09:50AM (#10909597)
They avoid using exceptions, which means avoiding doing work in constructors, which compromises your ability to use RAII, which in turn makes template-style programming much less effective.