Live data from Hacker News

Talking to C Programmers about C++ [video]

youtube.com

91–100 of 128 posts

Re: Talking to C Programmers about C++ [video]

#91
post #44

Earlier quoted context omitted.

>>A vector/map of what? How do I have a map of some key to a custom structure? How do you do this simply without templates? Separate libraries? Macro hell? Separate libraries, without "macro hell". >>How do you handle types which need to free memory without destructors? Manually loop over the vector and free stuff every time one goes out of scope? That doesn't seem safer to me. Providing both heap and stack based all…

Yes, but it would still be hard to support custom types, wouldn't it? Ie in your library, I would have to use pointers, which might be unsafe? Adding types to a library like that (like double) causes the amount of code to grow dramatically (maybe even n^2 for maps). You're library does look nice for what it does, and may have to look into it for some projects (I do have some C projects). Thanks!

That library is still in beta stage, there are others.

For maps I've implemented integer, pointers, and strings, without code bloat (tree implementation is common, without macro-templates). I'm planning adding some generic type support for fixed-size elements, while keeping internal tree pointer-less and both stack and heap allocation possible, with minimal code bloat/duplication.

Re: Talking to C Programmers about C++ [video]

#92

The problem with C++ is the constant introduction of unneeded features that are infinity obscure in necessity. People will say "Oh just don't use that feature" but that's not how this works. If something is there it is used. In reference to C, I don't think I know of a single feature or release that had major changes in how I wrote C. Maybe C99 and allowing me to declare in a for loop. I've been using C++ for class t…

I've been using C++ for a long time (10+ years). At one point (around C++11), I decided to really embrace the advanced features (template metaprogramming mostly) and stop trying to make the language something it wasn't. It can be complicated, for sure. But there is a certain plesantness that comes with letting the compiler do a lot of work for you. It can almost be like the switch to python - just let the compiler fi…

I mean I've been programming for some time and I come from a strong Java background. I get the basis of what C++ is trying to do with their new features but I think it's horribly executed when looked at in comparison to other modern languages like Java and Rust.

Speaking from a very strong Java background after Jigsaw I think Java will outdo the generics capabilities present in C++ in every way. Java 9 is bringing some crazy features to the mix. From what I understand they are getting rid of the boxing to object from generics to keep types static through lists and other generic elements. That's amazing and something C++ still does as templates are basically massively duplicated code in your binary while the JVM will get this for free at runtime.

Rust is also just out of left field as they are doing amazing things with their "cost-free abstraction"

Re: Talking to C Programmers about C++ [video]

#93
post #87

Earlier quoted context omitted.

So my opinion on the difficulty of learning features in a language are not valid since I'm just learning the features? Bravo. My problem is not with what I think you are attributing to me but rather other issues with the language and tool chain. There seems to me very little that can be done in C++ that can't be done cleaner in C. It's evident that most people who use a language aren't going to have time to spend yea…

There are many insightful comments in this thread (as well as others) that may provide arguments you are looking for.

I've been reading some of them through and some of them are actually fairly well rounded. Let's see how they respond to the questions I've posed to them.

I'm specifically interested in the RAII question to see how they justify fundamentally different behavior for syntactically similar code.

Re: Talking to C Programmers about C++ [video]

#95
post #37

Earlier quoted context omitted.

When I think about features in C++ that I would love to have in C, the only thing that comes to mind is constructors and deconstructions for RAII.

My issue with RAII is that it's fundamentally not what you expect to happen. If I say: int i; I don't expect that to create an integer. I expect that to reserve memory in the program's space for an integer. In the same way I don't expect this to call malloc: int *i; I just expect that to hold the space in the program data where and int pointer can be stored. Furthermore I don't expect this to run any code (just as th…

Hi gravypod.

It depends. Oh I hate that word.

When I see int i; without context I don't know anything about where memory is allocated.

It can either be the following. 1) Stack allocated 2) Heap allocated 3) Is in the read only data segment.

Just starting with a int, it can be any of the above three, two that reserve the memory on the heap/stack and another that is only a read only part of memory. The heap is the only place where I would need to call malloc/free.

Though for most common libraries when I see string s; They mostly have a pointer null terminated string in memory, and the primitive data types residing as the member's of string.

RAII helps in the situation where instead of having to do tedious code such as.

String s; allocate_string(&s);

// do something with string. free_string(&s);

It can all be done via a standard convention of calling the deconstructor or constructor to initialise and free itself.

Re: Talking to C Programmers about C++ [video]

#96

C++ is really hard. I have learned enough about it to understand that there's a lot of idiomatic ways of achieving things that I do not understand, especially with the latest standards. When someone claims to know C++, especially in a recruitment context, I tend to challenge that, since it's really hard to actually believe these days. Some people claim to know C++, but basically just write everything as if it was C.

>Some people claim to know C++, but basically just write everything as if it was C.

Nothing wrong with that; that's not uncommon in embedded systems development.

Re: Talking to C Programmers about C++ [video]

#97

Earlier quoted context omitted.

There are templates: for example void print1(auto x){std::cout is a function template template void print1(T x){std::cout This is not valid C++14 however, so it doesn't compile. If you replace these, you'll probably still get a stack overflow while expanding the templates because there are almost 7000 arguments to a single function call.

>This is not valid C++14 however, so it doesn't compile. It is "valid C++" IF there are fewer arguments, like 6-10 The example illustrates C++ internal representation of "variadic functions".

I was referring to the use of generic functions (ie. with an argument with type-specifier auto) which is not C++14. Your copy of g++ may accept it as an extension.

Re: Talking to C Programmers about C++ [video]

#98

Earlier quoted context omitted.

> what the fuck cout I'm not sure where that conversation would go. It prints to stdout. Would the student say the same thing about printf("%d", x)? How far down the rabbit hole did he want to go? Stream insertion syntax os pretty bad, though, I'll agree with that. Especially when you want to print floating point.

The conversation then shifts to how it does this? What if I want to print to something else? Why if I want to read differently from a stream? When you provide a shorthand that's all well and good but when you TEACH a short hand it's what's ONLY used by the students. The students will assume there is no other way to read and write then using >> and So many of the students in my CS class this semester can't do the firs…

I will agree with the point that the other methods of reading/writing from streams aren't taught early enough, and that formatted output is pretty bad with streams. I've actually written a wrapper around snprintf for printing in c++ (I should put that on github sometime)

Your project looks good. Two minor observations: 1.) You have 'using' statements in a header file. This is fine for a small project, but is considered bad form in larger projects (every file that includes that header will end up including the 'using' statements. This can cause collisions in large projects).

2.) Const correctness is a good habit to get into. (Ie, only pass non-const references if you are actually changing the object in the function)

Re: Talking to C Programmers about C++ [video]

#99

Earlier quoted context omitted.

So my opinion on the difficulty of learning features in a language are not valid since I'm just learning the features? Bravo. My problem is not with what I think you are attributing to me but rather other issues with the language and tool chain. There seems to me very little that can be done in C++ that can't be done cleaner in C. It's evident that most people who use a language aren't going to have time to spend yea…

Well you didn't express your criticism a the reason C++ is difficult to learn, you described it as "the problem with C++" and it sounds like you don't have enough familiarity with the language to make that sort of assessment. C++ isn't a great choice for many purposes, but for some purposes it is the go-to language - and for good reasons, not just because of inertia as some like to suggest. As with all forms of craft…

Here is why C++ is difficult to learn. It's like my experience of dealing with Java's inherited state except at every point in the language.

For people who have never touched java's inherited state from extending abstracts or base classes please don't fall into the traps I did. I was writing a redundant networking library that was meant to load packets on the fly so I had some cleaver "c++ level" abstraction written out for it. It only made sense to me because I relied on horribly complex features of Java. I'd say the most complex feature of Java.

Read through this to get an idea of what's going on. You'll notice that this is one of the sections with the least detail in the entire Java Primer thing. This is because in my opinion even the Java developers don't know all the rules.

https://docs.oracle.com/javase/tutorial/java/IandI/multiplei...

Now I don't think I know these rules either. I spent 2 months sorting out this mess and ended up with a polished product. But guess what? I could never go back there and fix something "quick". I'd need to do a bit of reading, read through my source, experiment a bit. This is not productive nor good standards and because of this I try never to rely on inherited state in Java anymore.

The same goes for every pain point of C++. Sure an expert, someone who lives, breaths, and dies by C++ might know exactly what is going on from the get go but that can't just be the assumption for every programmer. It is my opinion that if a language designer expects someone to read up on the language before going in and changing things they have failed at one of the cornerstones of language deign and implementation.

Everything should be "of course" levels of ingenuity but in C++ everything is "what the fuck".

For instance, please read my comment on RAII.

You can also take the multiple inheritance problems I've had in Java and directly apply them to C++.

You've also got strange behavior, when coming from C, for dealing with strings. For instance, where does an std::string get allocated? Is it on the heap or stack?

For instance am I opening myself up to bugs by doing this in my latest CS lab?

https://git.gravypod.com/gravypod/school/blob/master/cs280/h...

There's no apparent flow or rhyme to anything in this language. It just seems to be complexity bolted on simplicity and called "an industry standard".

Also operator overloading. I'd say it's a great thing for defining numeric and mathematical constructs that actually implement the specific operators as you would expect. For instance speaking as someone who has implemented their own basic physics library in java that is a godsend. But why, in the name of god, would you ever think of using numeric operators on non-numeric constructs? That's just inconsistency again leading down the further train of C++ needing to be learning in full before used.

Further, namespacing is taught poorly. I've had to do my own reading and settle on a standard that I think is semi-sane at least.

Rather then using the entire std namespace the same as my class I've settled on specifying namespaced elements I want to use. This is not how anything is taught in any college I've examined.

An example of what I do:

https://git.gravypod.com/gravypod/school/blob/master/cs280/h...

What's the point of using a namespace if you are just going to blanket import the entire namespace? Also if that's not the SOP for industry why is it taught as such in college?

There are many many if-ands-and butts about C++ and I can't go into all of them as I don't know all of them.

I do like reference types though, that's nice.

Re: Talking to C Programmers about C++ [video]

#100
post #87

Earlier quoted context omitted.

There are many insightful comments in this thread (as well as others) that may provide arguments you are looking for.

I've been reading some of them through and some of them are actually fairly well rounded. Let's see how they respond to the questions I've posed to them. I'm specifically interested in the RAII question to see how they justify fundamentally different behavior for syntactically similar code.

You seem to dislike the idea of the default constructor, not RAII. Default constructors, at least for relatively simple types, are meant to be minimalist so as not to incur any significant overhead. Which makes them comparable in this regard to the "default constructor" of 'int'.
Post reply on HN