5 ms·
Every language has inconsistencies, and C is not stranger to that. Much of c++’s baggage is due to C and you carry the same weight. That’s not to say that initi
by maccard 2y ago
Every language has inconsistencies, and C is not stranger to that. Much of c++’s baggage is due to C and you carry the same weight. That’s not to say that initialization isn’t broken in C++, but just like many features in many languages (off the top of my head in C - strcpy, sprintf, ctime are like hand grenades with the pin pre pulled for you) don’t use them. There’s a subset of C++17 that to me solves so many issues with C and C++ that it just makes sense to use. An example from a codebase I spend a lot of time in is
int val;
bool valueSet = getFoo(&val);
if (valueSet) {}
printf(“%d”, val); // oops
The bug can be avoided entirely with C++
if (int val; getFoo(&val)) // if you control getFoo this could be a reference which makes the null check in getFoo a compile time check
{}
printf(“%d”, val); // this doesn’t compile.
- ablob 2y agoYou could write this in C, no? { int val; if (getFoo(&val)) { ... }} Both ways of expressing this are weird, but stating that this can't be achieved with C is dishonest in my opinion.
- i_am_a_peasant 2y agoit's not that weird to explicitly limit the scope of certain variables that logically belong together. But i agree the C++ if(init;cond) thing was new to me.
- maccard 2y agoLike everything in C++, it has it's share of footguns if (Foo* f = GetPtr(); f->HasValue()) {} // wrong; f can be null vs if (Foo* f = GetPtr(); f && f->HasValue()){} Is probably the biggest pitfall. Especially if you're used to this: if (Foo* f = GetPtr()) { f->DoTheThing(); // this is perfectly safe. }
- maccard 2y agoIf I reformat this, { int val; if (getFoo(&val)) { } printf("%d", val); } The bug is still possible, as you've introduced an extra scope that doesn't exist in the C++ version. Also, this was one example. There are plenty of other examples.
- ho_schi 2y agoFor C users. And C++ users: In C++ we can declare variable in the while or if statement: https://en.cppreference.com/w/cpp/language/while https://en.cppreference.com/w/cpp/language/while https://en.cppreference.com/w/cpp/language/if https://en.cppreference.com/w/cpp/language/if It's value is the value of the decision. This is not possible with C [1]. Since C++17 the if condition can contain an initializer: Ctrl+F if statements with initializer https://en.cppreference.com/w/cpp/language/if https://en.cppreference.com/w/cpp/language/if Which sounds like the same? Now you can declare a variable and it value is not directly evaluated, you also can compare it in a condition. I think both are neat features of C++, without adding complexity. [1] Also not possible with Java.
- trelane 2y agoDeclaring a variable in a loop or if statement is supported since C99: https://en.wikipedia.org/wiki/C99 https://en.wikipedia.org/wiki/C99 Also in Java: https://www.geeksforgeeks.org/for-loop-java-important-points/ https://www.geeksforgeeks.org/for-loop-java-important-points...
- maccard 2y agoNo, declaring a variable in a `for` loop is supported in C99 but you can't do if-init.
- trelane 2y agoAh, you're right (finally tried it out). But it 100% works in a loop (usually for loop)
- maccard 2y agoIt’s one of those things that I never knew I wanted until I started using it, and now i miss it when it’s not available.The reason you want it is the same reason you want to declare a variable in the statement of a for loop rather than pre declaring and using a while loop
- tialaramex 2y agoVariables like "valueSet" scream out that the language lacks a Maybe type instead. One of the worst things about C++ is that it's content to basically not bother improving on the C type system.
- rowanG077 2y agoC++ has a maybe type. It's called std::optional.
- uecker 2y agoHere is my experimental maybe type for C: https://godbolt.org/z/YxnsY7Ted https://godbolt.org/z/YxnsY7Ted
- tialaramex 2y agoSix divided by minus one is a "Division by zero" now? Where I come from that's minus six. Good luck to WG14 (or maybe a faction within it?) as they seem to have decided to go make their own C++ competitor now, it's a weird time to do that, but everybody needs a hobby.
- uecker 2y agoYeah, I was about to add the test for INT_MIN / -1 but got distracted, but also not really the point of the example anyway. I hope you realize that this is example does not need any complex C++ features.
- tialaramex 2y agoI mean, sure. I read the "noplate" code. Did you ever watch the Mrs Merton show? "So, what first attracted you to the millionaire Paul Daniels?". There's a reason you felt the need to insist that your C language generic containers aren't relying on "complex C++ features" whatever you might decide that means.
- uecker 2y agoThis will be in C2Y and is already supported by GCC 15: https://godbolt.org/z/szb5bovxq https://godbolt.org/z/szb5bovxq
- maccard 2y agoThis is one example. Off the top of my head std.array vs "naked" C arrays, string vs const char*, and let's not forget RAII are all features that just make me never want to work with vanilla C ever again.
- uecker 2y agoFor me, std.array seem fundamentally inferior compared to C arrays. A good standard string type is indeed missing, but it is also easy to define one. RAII, I can see, but I also some advantages to have explicit resource deallocation visible in the code and it is not really bothering me too much to write this explicitly.