News for hackers. Stuff that matters.
Re:The Unreasonable Effectiveness of C by AshleysBrain on Thursday January 10, 2013 @10:45AM (#5037481)
I always get bashed for saying I like C++, but I genuinely don't get how C programmers manage without code like this:

    std::map<std::string, std::vector<std::string>> foo;
One line and you've set up a nontrivial data structure with automatic memory management. No macro horrors (which are a diabolical way of implementing what C++ templates do well).

Since you can code like C in C++, I'm not sure why more people don't use C-with-templates as a programming style.

Re:The Unreasonable Effectiveness of C by TillE on Thursday January 10, 2013 @12:17PM (#5038219)
This is precisely my problem with C, and why I hate using it. Data structures are incredibly tedious.

Whenever I see younger people gushing about C, I can't help but wonder if they've actually written anything nontrivial with it. When used properly, C++ is a vastly more productive language.

Re:The Unreasonable Effectiveness of C by xamuel on Thursday January 10, 2013 @11:11AM (#5037681)
C guy here. I'm assuming you're serious and not being sarcastic... which is a nontrivial assumption, because that line you posted looks like something out of the foul depths of hell.

You aren't going to run out of lines any time soon. Who cares whether your data structure definition is 1 line or 5?

Re:The Unreasonable Effectiveness of C by hackinthebochs on Thursday January 10, 2013 @11:49AM (#5037988)
> because that line you posted looks like something out of the foul depths of hell.

I must say I laughed-out-loud at that, mainly because I've been on both side of the divide when it comes to opinions on verbose std definitions. It is an odd feeling to simultaneously feel revulsion and nostalgia towards a line of code.

I do think there is a non-trivial, and sometimes massive boon gained from concise definitions. It's the difference between an acronym in natural language vs referencing a concept by its full verbose name. The shorter the definition of a concept, the fewer units used by your working memory when referencing that concept, thus freeing your mind to higher level considerations.

Re:The Unreasonable Effectiveness of C by to3m on Thursday January 10, 2013 @12:27PM (#5038307)
I agree - I've found the STL a massive productivity boost, for all its ugliness. It's worth it for std::vector and std::string alone, and I also like std::map (even though the API teeters on the boundary between genius and insanity) and std::set. All there for your use, with your own data types, out of the box.

I discovered the STL one day in February 1999, by accident, when looking for something else in the VC++ help. Once I realised what I'd found, I gave up C entirely within about 1 week.

Re:The Unreasonable Effectiveness of C by larskinn on Thursday January 10, 2013 @11:18AM (#5037747)
Foul depths of hell? I've never programmed in C or C++, but this seems to me to be a map from strings to vectors of strings. Reads easily. I don't like the :: namespace separator, but what can you do...
Re:The Unreasonable Effectiveness of C by abrichr on Thursday January 10, 2013 @11:42AM (#5037927)
but what can you do...

    using namespace std;

    map<string, vector<string>> foo;
Re:The Unreasonable Effectiveness of C by luser001 on Thursday January 10, 2013 @12:47PM (#5038453)
I used to do this. But then I realized I would be importing all symbols from std into my code. Including ones that might get defined in the future (similar to python's 'from foo import *'), which might conflict with some of my own symbols.

Hence, I now implicitly import each symbol I need with something like 'using std::map;' (similar to python's 'from foo import bar, bar2').

I've found that the 'using' statement can be used even inside a function to restrict the importation to just a single function.

FYI.

Re:The Unreasonable Effectiveness of C by anonymouz on Thursday January 10, 2013 @01:23PM (#5038698)
That's good advice. But even when importing all of std, at least namespace collisions in C++ will result in some somewhat sane compiler error.

I not so fondly remember a day hunting down a cryptic C compiler error coming from some header that had

      #define F1 ...,
replacing F1 in my file by some cryptic mess...
Re:The Unreasonable Effectiveness of C by abrichr on Thursday January 10, 2013 @02:47PM (#5039291)
Interesting; I never thought of that analogy to Python. I've just had it drilled into my head from everything I've ever read about C++ that "using namespace std" is terrible, and I shouldn't use it.

Great advice. Thanks!

Re:The Unreasonable Effectiveness of C by yuushi on Thursday January 10, 2013 @08:50PM (#5040911)
Except the analogy to Python lacks one critical point: C++ headers and Python imports do not work in nearly the same way. Do not EVER use a "using namespace <foo>" in a header file; any code that #include's such a header will pull this in.

In .cpp files it's ok (and almost required if you're using boost, unless you want boost::foo:bar::type everywhere). It still requires a bit of thought, though.

Re:The Unreasonable Effectiveness of C by popee on Thursday January 10, 2013 @01:47PM (#5038885)
It's ugly to see shift operator here? :-)
Re:The Unreasonable Effectiveness of C by Saavedro on Thursday January 10, 2013 @03:47PM (#5039635)
Unless you're on C++11 you'll be required to shove a space in there to get it to compile anyway ;)
Re:The Unreasonable Effectiveness of C by pcwalton on Thursday January 10, 2013 @12:15PM (#5038192)
Binary search trees cannot be implemented in 5 (reasonably-wide) lines of C.
Re:The Unreasonable Effectiveness of C by waffenklang on Thursday January 10, 2013 @12:25PM (#5038282)
And can't be _implemented_ in any language. C gives you at least the charming fact that you can implement one without any overhead of objects, classes, implicit memory management or some other concept, just having a lightweight implementation that you may use in an environment without any libraries, like std or else..
Re:The Unreasonable Effectiveness of C by tyree732 on Thursday January 10, 2013 @02:00PM (#5038971)
So what you're saying is that C lets you write it on your own. In fact, and this might sound crazy, but you can do that exact same thing in C++, it's just that people don't because the C++ standard library provides plenty of performant and featureful data structures for you.

Besides, anything the C++ standard template library map does when implementing a red-black tree is exactly what you would need to do if you wrote your own, minus having things like allocators (which are incredibly useful for those that need them). You speak of overhead due to "objects, classes and implicit memory management". Inevitably, any implementation of a data structure of substance in C ends up looking like c++-ified C. Want to create your hashtable? Call hashtable_init and get back a hashtable_t. Want to insert into it? Pass your hashtable_t, a key and a value into hashtable_insert. Even worse, you either have the choice of making all of your data void* in the data structure and casting into and out of your interface or making awful macros to define the various methods and structures for the types you would like to use. It's like you're stubbornly writing C++ in C.

Re:The Unreasonable Effectiveness of C by deelowe on Thursday January 10, 2013 @02:27PM (#5039151)
I'm confused.

C has libraries too. I personally like the headers used by openbsd: http://www.openbsd.org/cgi-bin/cvsweb/src/sys/sys/tree.h?rev...

Re:The Unreasonable Effectiveness of C by YZF on Thursday January 10, 2013 @10:01PM (#5041114)
I'm not sure why I'm getting sucked into this again :-) but it doesn't have standard, portable libraries. It also doesn't have the right abstractions in the language to provide clean and safe (i.e. no void* and no brittle preprocessor magic) implementations of such.

I've written C for most of my life, you can do great things with it. I also have a love/hate relationship with C++ and Python. There is no perfect language, they all have to make tradeoffs.

Re:The Unreasonable Effectiveness of C by waffenklang on Thursday January 10, 2013 @06:25PM (#5040395)
yes, even standard libs, but they arent that promoted as c++ std libs, as most of them are just apis for system interaction, only a minority are useful out of the box algorithms.

and third party libs are also possible, yes.

Re:The Unreasonable Effectiveness of C by waffenklang on Thursday January 10, 2013 @06:20PM (#5040374)
no its not c++ish, its simply an implementation which is technically similiar to c++, because they are at some core syntax/semantic similar. i didnt said that its the new wheel, or better or worse. its just free of third party stuff, which will be compilable anywhere only dependent of the compiler used. and i didnt exclude that this is exlusive for C, sure this applys to c++ as well. but not if you want to use feature complete c++.
Re:The Unreasonable Effectiveness of C by caf on Thursday January 10, 2013 @09:05PM (#5040969)
You do not need to cast to or from `void *` in C, and it's not idiomatic to do so.
Re:The Unreasonable Effectiveness of C by AshleysBrain on Thursday January 10, 2013 @12:35PM (#5038369)
Well, what I posted wouldn't be in a tidy codebase. You'd have something like:

    // foo.h
    typedef std::map<std::string, std::vector<std::string>> StringToVectorString;
    
    // foo.cpp
    // in several places you can just use:
    StringToVectorString foo;
Re:The Unreasonable Effectiveness of C by VMG on Thursday January 10, 2013 @01:05PM (#5038562)
> Who cares whether your data structure definition is 1 line or 5?

Why not use assembler and make it 50?

Re:The Unreasonable Effectiveness of C by stinos on Thursday January 10, 2013 @12:11PM (#5038156)
well the thing is, it also works for std::vector<string>, or std::vector<int>, or anything you want. Last time I checked in C you'd have to either duplicate all your array code for each type or resort to something like void*. Only for that I'd use C++ (yes, you can use it without classes if you want).
Re:The Unreasonable Effectiveness of C by digeridoo on Thursday January 10, 2013 @12:51PM (#5038476)
Last time I checked in C, all pointer types where the same storage size. There's no need to define the same data structure multiple times by using templates. You just use casts.
Re:The Unreasonable Effectiveness of C by sbmassey on Thursday January 10, 2013 @02:20PM (#5039093)
... which is fine for creating data structures containing pointers to things created elsewhere, but it doesn't work if you want your data structures to contain more complex data types, without the overhead of another layer of pointer redirections.
Re:The Unreasonable Effectiveness of C by spc476 on Thursday January 10, 2013 @02:30PM (#5039175)
Nope. There is no need in C for all pointers to be the same size. An implementation is free (and there indeed, exist some) where a char* is a different size from an int. You can* safely cast any data pointer to void, and a void back to any data pointer. POSIX requires that function pointers can do this; the C standard doesn't require it.

Now, that said, it is possible to write generic link list routines (I've done it) but the devil is in the details.

Re:The Unreasonable Effectiveness of C by wanderingmarker on Thursday January 10, 2013 @11:10AM (#5037670)
In the past, when I worked on a C++98 project, I would have disagreed with you for one simple reason: template debugging sucks. gdb did not provide good template information (but may have improved in the last few years, I have not checked it recently). g++ had horrific template error messages.

Today, I'm looking at C++11 and using Clang, and I like what I see. Vastly improved compiler error messages, lambdas, very decent type inference, standardized smart pointers, good standard library (and Boost is always an option). I haven't tried heavy-duty debugging yet, but I suspect it still sucks. Overall — I'll probably be using C++ quite a bit more in the future. It is no longer the nasty pile of kludges on top of C which it used to be ten years ago.

Re:The Unreasonable Effectiveness of C by 16s on Thursday January 10, 2013 @01:05PM (#5038567)
I up-voted you because I agree with you. And let me tell you that in the "real world" of systems programming (where stuff gets done), C++ is widely used and loved by engineers. It's not hip and popular to say that, but it's true from what I have seen over the last 15 years.
Re:The Unreasonable Effectiveness of C by jmaygarden on Thursday January 10, 2013 @11:43AM (#5037937)
Does that actually compile in C++11? I haven't used C++ in a while, but you used to need whitespace to separate the two closing angle brackets (i.e. ">>" should be "> >").

Personally, I use C for my embedded work and Python for test applications and data processing. C++ is in the middle and I haven't had a use for it.

Re:The Unreasonable Effectiveness of C by AshleysBrain on Thursday January 10, 2013 @12:30PM (#5038336)
A few compilers even before C++11 treated >> in template definitions as expected (e.g. VS2010) even though it wasn't standard, then the C++11 standard officially made it required. So I haven't found the >> thing to be a problem for a while now.
Re:The Unreasonable Effectiveness of C by coffeeaddicted on Thursday January 10, 2013 @11:58AM (#5038068)
Yes, that got changed/fixed with c++11. So you can now use >> there.
Re:The Unreasonable Effectiveness of C by mbq on Thursday January 10, 2013 @12:00PM (#5038086)
I bet in 99% of cases using such structure is a nonsense overhead, and in 85% the code relaying on it can be written without a single heap allocation.
Re:The Unreasonable Effectiveness of C by to3m on Thursday January 10, 2013 @12:14PM (#5038191)
One man's nonsense overhead is another's labour-saving modern convenience ;)

(I like to write my code without heap allocations, but when you need an array of arrays of strings, indexed by another string, it doesn't half get a bit annoying to code up.)

Re:The Unreasonable Effectiveness of C by unphased on Thursday January 10, 2013 @04:38PM (#5039934)
What is equally powerful about modern C++ (quite possible to make it work on C++98 as well) is the ability to have template methods that can dump out pretty-printed fully customizable (by overloading operator << in conjunction with a stringstream) debugging for data structures.

Yes, template programming is not something I am likely to ever figure out but these things only need to be built once, by somebody other than me.

Re:The Unreasonable Effectiveness of C by p0nce on Thursday January 10, 2013 @02:43PM (#5039257)
But... you could have done it with a linked list!