Hacker News new | past | comments | ask | show | jobs | submit login

Everytime I need to write C, I wish I could use C++ instead. The things I miss most are RAII, templates, lambdas, const-correctness, and most importantly, a standard library with container types and algorithms.



What is wrong with const-correctness in C?

I think container types and algorithms is a fair point, but if you program C more you should have a go-to library or your own implementation.


> What is wrong with const-correctness in C?

One problem is that most C compilers accept implicit conversion between unrelated pointer types by default. If you accidentally pass a `const T*` to a function that takes a `T*`, you only get a warning. In C++ this is always a compiler error.

Another thing that I find very annoying (that is more about `const` in general): in C you cannot use `const` variables as compile time constants, instead you always have to use the preprocessor.

> but if you program C more you should have a go-to library or your own implementation.

Sure, but I still don't like the fact I need to find a third-party library (or roll my own) for the most basic data structures. Also, it's not like generic containers can be trivially implemented in C. They require lots of preprocessor magic and the resulting API will always be much worse than any equivalent C++ implementation, in particular with non-trivial types.


You can build with -Werror like the rest of us to get a compiler error instead of a warning.

As for compiler time constants, you certainly can use them for those, but the compiler will happily identify constants without the const keyword, whose true purpose is to cause a build time error if you try to modify it.

Regarding libraries, see my other reply:

https://news.ycombinator.com/item?id=42506570

Libuutil is a system library that is on any system that uses ZFS, although there are no known external consumers, so if you are interested in being the first, feel free to open bug reports with OpenZFS asking for removed functionality to be restored if you want any of it.


> As for compiler time constants, you certainly can use them for those

No, you can't. In C, `const` variables are not constant expressions. This means you can't use them in case labels or to define the size of an array.

> Regarding libraries, see my other reply:

I know that there are several container libraries for C. I was only complaining that there is no standardized solution for the most basic data structures.


What you want is something is is exclusively for compiler time constants, rather than can merely use compiler time constants. I know I am being pedantic here, but that is an important distinction to make when dealing with computer programming languages, since the descriptions need to be free of ambiguity or there will be problems. What you had meant was unclear to me in your previous remarks. I understand now.


There is the trick: use enum for const. It works most of the time.


Yes, at least that's better than macros.


As for not having a standardized solution, sys/queue.h is the closest you will get to it. It would be nice if we had one, but it has not happened yet.


I do not see how this not being an error is a problem. First, you can usually toggle this via a compiler flag. Second, I personally find it very convenient that the compiler does not stop for such things. This gives me more freedom how I can do my work, e.g. fixing const warning or do some other change first.

I do not agree about the APIs being worse than C++.


You should see the libuutil library that Sun made to provide containers and other niceties. Even without it, there is always sys/queue.h.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: