News for hackers. Stuff that matters.
Re:What if anything have we learned from C++? [video] by infradig on Friday July 24, 2015 @03:23AM (#9940534)
After twenty years of using it? That I was wrong, that plain-old C really is better.
Re:What if anything have we learned from C++? [video] by AdieuToLogic on Friday July 24, 2015 @04:16AM (#9940696)
The C that you call "really is better" is not the C which existed before C++. It is "K&R C", which looked like this:

  foo (a, b, c)
  char *a;
  int b, c;
  {
     /* do something cool here */
  }
And there were no "function declarations" (who needs argument type and cardinality checking anyway), no "slash slash" single line comments, no need to declare a return type as int was implied, say goodbye to const and volatile qualifiers, any char array is mutable (including those defined in double quotes), plus be sure to use function pointers thusly:

  (*some_pointer) (insert, args, here);
Instead of:

  some_pointer (looks, like, a, function);
Oh yeah, and say adios to struct initialization using the C89 syntax. And it's a no-no to try and initialize a local array along the lines of:

  char *strings = {
    "this", "is", "convenient", "isn't", "it?"
    };
So if you wish to assert (pun intended) "that plain-old C really is better", then be sure to acknowledge where a non-trivial amount of what is known as "plain-old C" stems from.
Re:What if anything have we learned from C++? [video] by exacube on Friday July 24, 2015 @04:20AM (#9940710)
by plain old, i think infradig was implying that C was plain and old compared to C++, and not about an older standard of C.
Re:What if anything have we learned from C++? [video] by ectoplasm on Friday July 24, 2015 @04:27AM (#9940721)
Yes, plain-old C like plain-old vanilla ice cream, as compared to C++ like swirly rainbow vanilla chocolate strawberry template macro class inherits from a waffle cone with generic unique pointer functor sprinkles ice cream.
Re:What if anything have we learned from C++? [video] by blub on Friday July 24, 2015 @06:03AM (#9940989)
I hold the exact opposite opinion, and would never want to work on a plain C project. It is so low level that one has to write a lot of plumbing code to take care of error handling and resource management. It's also missing pretty much all higher-level concepts that are fast and can make code easier to write, read and less error-prone. Overall, it's simply not fun to write C code, there's a lot of tedious manual work, and even if you do everything according to the book you might oversee something and have problems. Well-written C++ completely eliminates some classes of problems, just like Rust's type system can eliminate even more issues.

C is so tedious to write that even if I didn't have access to C++ I would try something else first when the need to write cross-platform/performant code would come.

The point is: it's not appropriate to evangelise C (or any other language) without pointing out some of its major disadvantages. This is simply spreading misinformation.

Re:What if anything have we learned from C++? [video] by xamuel on Friday July 24, 2015 @07:41AM (#9941264)
>plumbing code to take care of error handling

This becomes much simpler once you learn how to write your own variadic functions (it's not that hard, though I do admit it could be prettier). Then you just write one single logerrf function or whatever, which 9 times out of 10 you can carry with minor modifications to your next project. Java does try/catch better in the sense that with checked exceptions you can force library users to at least acknowledge exceptions. Without checked exceptions, try/catch just hides the extra int *err argument under the rug, actually increasing the risk of library users not acknowledging edge cases.

>and resource management

I don't like C++ malloc'ing things behind my back. I see it as trading maintainability for instant gratification. The problem is compounded by the way C++ doesn't play nice with gdb/valgrind. Memory leaks and other memory errors are much easier to fix in C than C++.

>C is so tedious

It really isn't, though, if you use it right. Whatever syntactic tedium it has is more than compensated for by the lightning-fast compile time and the infinitely simpler compiler errors (due to no overloading, no templates...)

Re:What if anything have we learned from C++? [video] by restalis on Friday July 24, 2015 @10:21AM (#9942138)
"I don't like C++ malloc'ing things behind my back. I see it as trading maintainability for instant gratification."

I agree and I want to add that for a language "with a bias towards system programming" C++ makes strange assumptions about the memory. It assumes that there is only one kind of memory available for every operation (and it does its thing with that by default). Then it goes and further assumes that there is only one way (no finer further details whatsoever) of getting that one and only type memory. It's exactly the one boot fits all kind of thinking that Mr. Stroustrup suggests that C++ hasn't.

Re:What if anything have we learned from C++? [video] by agumonkey on Friday July 24, 2015 @06:46AM (#9941094)
Stroustrup gave talks where he explained how to cherry pick c++ elements to decorate simple C code with just enough abstraction to get done safe and fast. I find it's an old timer skill to be able to use languages as suggestions rather than law and to strip it using only the bits that will really bring value to the project.
Re:What if anything have we learned from C++? [video] by eots on Friday July 24, 2015 @04:37AM (#9940753)
What I've learnt is that if you start writing C++ as a plan-old C, you quickly find yourself reaching for this and that feature from C++ that makes your job easier and your code more readable. Knowing where to stop is the tricky part.
Re:What if anything have we learned from C++? [video] by bluetomcat on Friday July 24, 2015 @05:40AM (#9940921)
Coding in plain C99 with some discipline can actually get you most of the benefits of C++, without any of its quirks.

The "object-orientedness" of C++ really boils down to implicitly passing the this pointer to member functions, inheritance + virtual functions, and namespaces. All of these can be emulated in what I would call "C with discipline". Just be consistent in your naming conventions, always use a common prefix for globally visible functions and types, and use structs of function pointers whenever you really need virtual functions.

Re:What if anything have we learned from C++? [video] by checker659 on Friday July 24, 2015 @05:45AM (#9940936)
What about templates? For some reason, C programmers only seem to comment about the OO parts of C++ and never talk about templates. When you bring variadic templates into the mix, it's just more awesomeness.

And, before you mention it, use clang to avoid getting the page long templates related errors.

Re:What if anything have we learned from C++? [video] by bluetomcat on Friday July 24, 2015 @06:19AM (#9941031)
I haven't seen much template wizardry in the C++ codebases I've worked on. It can generally be seen in the standard headers.

Type-generic data containers can still be implemented with void pointers and very little runtime overhead, for example: http://lxr.nginx.org/source/src/core/ngx_array.h

Re:What if anything have we learned from C++? [video] by detrino on Friday July 24, 2015 @07:35AM (#9941234)
You cannot implement cache efficient data structures using void pointers.
Re:What if anything have we learned from C++? [video] by f00fc0d3 on Friday July 24, 2015 @06:30AM (#9941055)
C99 is nice, but C still lacks generic. I am not talking about C++ template nonsense, but not having to write max element search for every primitive type.

I am working in DSP project which was started with typical C++ OO BS, finally with ended up with C compiled as C++ + some simple templates for cases as above.

Re:What if anything have we learned from C++? [video] by pjc50 on Friday July 24, 2015 @06:55AM (#9941120)
C has a generic: "void *". It just doesn't have any type safety.
Re:What if anything have we learned from C++? [video] by eutectic on Friday July 24, 2015 @08:17AM (#9941396)
It also has bad performance if you need to pass function pointers around.
Re:What if anything have we learned from C++? [video] by pjc50 on Friday July 24, 2015 @09:20AM (#9941746)
Is this noticeably worse than calling through the vtable?
Re:What if anything have we learned from C++? [video] by eots on Friday July 24, 2015 @10:16AM (#9942118)
In C++ generics don't involve calling through a vtable.
Re:What if anything have we learned from C++? [video] by ksherlock on Friday July 24, 2015 @08:26AM (#9941432)
C11 has type generic macros. You can build your own with the _Generic statement.

    int maxi(int, int);
    double: maxd(double, double);
    ...
    #define max(x) _Generic((x), int: maxi, double: maxd, ...default: maxi)
Re:What if anything have we learned from C++? [video] by f00fc0d3 on Friday July 24, 2015 @09:32AM (#9941824)
Yeah, but you still need to write those maxi, maxd :-(
Re:What if anything have we learned from C++? [video] by stinos on Friday July 24, 2015 @06:48AM (#9941099)
I am not talking about C++ template nonsense, but not having to write max element search for every primitive type

How is it template 'nonsense', when those templates do exactly that?

Re:What if anything have we learned from C++? [video] by f00fc0d3 on Friday July 24, 2015 @07:27AM (#9941197)
Template metaprograming is nonsense, not simple type substitution. C++ templates are just too much, especially when mixed with substandard developers.
Re:What if anything have we learned from C++? [video] by bluetomcat on Friday July 24, 2015 @06:42AM (#9941080)
Using statement expressions and the typeof extension, you are able to write function-like, type-generic, scope-insensitive macros like this one:

    #define MAX(a,b) ({ \
        typeof (a) _a = (a); \
        typeof (b) _b = (b); \
        _a > _b ? _a : _b; \
    })
The inability to write template functions can be compensated by designing your function so that it takes a function pointer where the type-specific action occurs.
Re:What if anything have we learned from C++? [video] by nly on Friday July 24, 2015 @07:55AM (#9941308)
Your MAX has no respect for namespaces or scopes. A real world macro is probably going to be called MYLIBRARY_MAX to avoid this. Secondly the C++ lambda solution is just easier to read.

    auto max = [](auto a, auto b) { 
        return (a > b) ? a : b;
    };

    max (3.1415, 42); // returns double
    max (42ul, 78u); // returns unsigned long
    max (17, 16); // returns int
Re:What if anything have we learned from C++? [video] by f00fc0d3 on Friday July 24, 2015 @07:25AM (#9941194)
Typeof is not standard and many DSP compilers do not implement that. Secondly I meant max search in array, not between to scalar values which is trivial. I really don't want to have for(...) inlined everywhere.
Re:What if anything have we learned from C++? [video] by madmoose on Friday July 24, 2015 @05:15AM (#9940848)
I like to call that "C with std::vector"
Re:What if anything have we learned from C++? [video] by pjc50 on Friday July 24, 2015 @07:01AM (#9941131)
I've spent most of my career writing C++ in the "C with classes and containers" style. Apart from the compile times and library issues, this really is better than just standard C. You can automate much of the memory management.
Re:What if anything have we learned from C++? [video] by o87dv on Friday July 24, 2015 @04:32AM (#9940733)
I'm a C programmer, and I'm considering trying C++ in the future. Can you elaborate?