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…
>For heap allocation, you would need to call some sometype_free(&a) function i find '__attribute__((cleanup (fn)))' fascinating in C, need to try it in some production code though.
Talking to C Programmers about C++ [video]
71–80 of 128 posts
Re: Talking to C Programmers about C++ [video]
#72Earlier quoted context omitted.
No. There's everything simple about Java. The semantics of Java are actually pretty simple, at least compared to C++. What's complicated are the monstrosities people build with Java, but those aren't inherent in the language. Saying that Java is complicated because EE exists is like saying that C is complicated because Linux exists: Complexity can be built atop simplicity. And for some reason, Java is a complexity ma…
> The semantics of Java are actually pretty simple, at least compared to C++. While true, have you ever tried to answer Java Puzzles?
Re: Talking to C Programmers about C++ [video]
#73Earlier quoted context omitted.
Well then, that's your problem: C++ is trying to be both low-level and high level at the same time . This isn't a good idea, IMHO. What I mean is that while some amount of RAII is a common idiom in C++, It's rare for a program to use RAII fully, as many C++ idioms conflict with it. Mind, I'm not an expert, so I might be wrong, but that was my understanding. AFAICT, if RAII was used all the time, idomatic C++ would lo…
> Well then, that's your problem: C++ is trying to be both low-level and high level at the same time. This isn't a good idea, IMHO. It is a very good idea and C++ isn't alone there. Professional Basic dialects, Turbo Pascal, Delphi, Modula-2, Modula-2+, Modula-3, Ada, D, Rust, Mesa/Cedar all share this idea that you can program at both levels, depending on the needs of the use case.
Re: Talking to C Programmers about C++ [video]
#74The 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…
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.
Re: Talking to C Programmers about C++ [video]
#75Earlier quoted context omitted.
It is one language. Many of those subsets are not mutually exclusive. Regardless of what your preference is, if you claim to know C++ you need to at least know some of its basic features, such as references, streams, templates, STL, etc. (features that have been around for a long time). And if you claim expertise, to at least be familiar with basic features from the new standards.
Your list of common features is a bit too broad. For instance a common sentiment among game programmers is "STL is shit, you better don't touch it at all".
Re: Talking to C Programmers about C++ [video]
#76Earlier quoted context omitted.
I'd be interested to hear why you say that RAII cannot be idiomatic. With `std::unique_ptr` and `std::shared_ptr` for handling pointers, `std::vector` for handling data arrays, `std::string` for handling char arrays, I would argue that modern C++ is very heavily RAII. At this point, if I have a class that cannot be declared on the stack, with cleanup handled by RAII, I consider it to be a broken class. Good point on…
The main issue with RAII is that you can only do properly if as you say, the classes were designed to be stack allocated. Also that no one just placed such class in the the heap, and it was missed, because no one is actually doing code reviews or making use of static analysis. Another issue with best practices and ownership are binary libraries. There isn't any sort of control one can have over them, so they are the…
auto deleter = [](unsafe_type* t) { cleanup_unsafe(t); }
std::unique_ptr safe(make_unsafe(), deleter);
If this is used in many places, you can make a very easy wrapper class to handle it. class safe_wrapper {
public:
safe_wrapper()
: unsafe(make_unsafe()) { }
~safe_wrapper() {
cleanup_unsafe();
}
unsafe_type* operator->() { return unsafe; }
private:
unsafe_type* unsafe;
};
(Note that the example there is only for use within a single function. If the safe_wrapper is to be returned from a function, then the copy/move operators should be defined as needed.)Certainly, when passing ownership back to the binary library, you are relying on it to correctly handle ownership. But so long as the ownership is in one's own code, you can easily add the type safety.
Regarding a stack-based class being accidentally placed on the heap, you can't prevent all errors, you can only make it less likely. I'd argue that it is easier to accidentally forget to call a cleanup function than it is to accidentally place something on the heap. With C++11 and up, any calls to "new" should be regarded with deep suspicion.
True on the FP patterns moving in, and I love them. `std::function` is a joy to work with, especially as compared to either C-style function pointers or inheritance from a "callable" type.
Re: Talking to C Programmers about C++ [video]
#77Earlier 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.
Problem is, using destructors (and RAII) may have, in general, certain performance implications that C may not be willing to deal with. This is somewhat similar to demanding that C had a garbage collector built in. Note that, being lazy, memory management by means of garbage collection may turn out being more efficient than using the (eager) destructors.
I've had some good/bad experiences with garbage collectors. They do help improve productivity and ROI within the enterprise space where memory isn't too much of of issue. Then when memory does become a issue within smaller memory footprint environments they can be a real pain in the ass.
Re: Talking to C Programmers about C++ [video]
#78The 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…
The problem with posts about C++ on Hacker News is that people who aren't experienced C++ programmers (you've been using C++ for a semester? do tell) feel compelled to write posts explaining the problems with C++.
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 years becoming a master at it. They are going to join a team and say "So to do my assigned task I need to do X, what features let me to X the cleanest" and this is how I've taken on learning C++.
Sadly, no features that are new have yielded themselves to any problem I've seen. I haven't encountered it.
Your solution to rebut my argument is that "you need to be at least 10 years into the language to know anything kid!" yet you provide no rebuttal.
Please do provide an argument not an insult at my age the next time you'd like to raise a disagreement with my ideas.
Re: Talking to C Programmers about C++ [video]
#79Earlier quoted context omitted.
This problem is not limited to HN, I see it everywhere. And wrt to the notion that every feature of C++ (from simple, to complex, to highly complex) will be used, I've been using C++ professionally for ~13 years and there are certain features I've chosen to stay away from since they don't add any benefit to the problems that I solve (or to the way I solve them). And I feel fine about that bc I'm very pleased with the…
As somebody who is relatively experienced with C++, but primarily on small projects with few developers, which features do you tend to stay away from?
Re: Talking to C Programmers about C++ [video]
#80Earlier quoted context omitted.
As someone who's had to deal with C++ since the last century, I find that one problem with C++ is that people who aren't experienced C++ programmers can't get shit done, especially in a code base where experienced C++ programmers have demonstrated their erudition and knowledge of C++ arcana in all its glory. In fact, the intersection of people who had the patience to learn enough C++ to get shit done and people who a…
> 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.
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 first lab, which in all essence, is a tokenizer because they don't know that there are other ways for writing to, or reading from, a stream!
A short hand should only be used in a majority if it can do everything the long hand way can do.
Also for anyone interested what my first C++ project looked like you can see here:
https://web.njit.edu/~gwryan/CS280/CS280-Program-1-Fall-2016...
https://git.gravypod.com/gravypod/school/tree/master/cs280/h...
It's not my best work, but if any C++ people want to notify me of things I'm doing that's wrong I'm more then happy to learn.