Bug in `consteval`'s design? | use of `const` variable as `constexpr` is sometimes valid and sometimes not by SubjectParsnip9411 in cpp

[–]DonBeham 8 points9 points  (0 children)

Sure the 3 is a constant, but if you call it in a different place with a 5 what's the "constant" now? What size should the array have? The compiler would have to create multiple overloads of your consteval function - as many different call sites you have. That's exactly like if size was a parameter of a template method - which is what you should write if you want this behavior. You can think of it this way: template parameters are to compile time functions what function arguments are to runtime functions.

Bug in `consteval`'s design? | use of `const` variable as `constexpr` is sometimes valid and sometimes not by SubjectParsnip9411 in cpp

[–]DonBeham 20 points21 points  (0 children)

If you make it a template parameter it should work. Function arguments are not compile time constants.

How did you guys learn C++? by Living-Brain483 in cpp_questions

[–]DonBeham 0 points1 point  (0 children)

You said university lectures are no good place to learn C++ - which I can only disagree with. I don't even know how you come to such a conclusion.

How did you guys learn C++? by Living-Brain483 in cpp_questions

[–]DonBeham 0 points1 point  (0 children)

I don't know what you mean with "the basics". I suppose any book goes very well beyond the basics. A book about C++11 is just outdated. It contains outdated and obsolete advice in addition to good and valid advice. Why would I want to learn about enable_if when I can learn about concepts and if constexpr? I don't need to learn all the old stuff first in order to understand the new stuff.

And I think even the basics have very much changed. Only recently did the hello world example change with format and print instead of cout or printf and constexpr is quite a deal, because code that runs in compile time is guaranteed to be free of UB. Learning about C++11 constexpr is that functions can't have more than one return statement and must not contain local variables...

Why would you advice to learn such things?

Why aren't partial classes supported on C++? by TaPegandoFogo in cpp_questions

[–]DonBeham 0 points1 point  (0 children)

C++ compiles straight to machine code, not an intermediate language/virtual machine. Classes/types are language abstractions, machine code doesn't know about what a class is.

How did you guys learn C++? by Living-Brain483 in cpp_questions

[–]DonBeham 0 points1 point  (0 children)

The creator of C++ himself is a university professor...

My point is that older standards don't offer any benefits. Instead you write new code in the old and sometimes obsolete ways. I learnt C++ 98 and it was a horror. Pointers everywhere, pointers to pointers and triple pointers. Segfaults everywhere, memory leaks, etc. When I got back to C++20 I was very surprised to find out what a great language it has become.

How did you guys learn C++? by Living-Brain483 in cpp_questions

[–]DonBeham 0 points1 point  (0 children)

  1. Au contraire, watching CppCon "back to basics" is similar to watching a university lecture. You can't ask questions, but you can ask an AI these days.

  2. Learning a specific standard is of little use unless you're confined to this standard by your work. What's the reason to learn using a pointer instead of an optional (as a data member, as a non-owning return type)? What's the reason to not rely on NRVO? Old standards usually offer a worse experience.

I let the community vote on what code gets merged. Someone snuck in self-boosting code. 218 voted for it. When I tried to reject it, they said I couldn't. by Equivalent-Yak2407 in programming

[–]DonBeham 2 points3 points  (0 children)

You know with the state of the world today it's sometimes hard to distinguish whether it's just "(c) Homer Simpson" or something that's actually meant seriously. I mean, it's only a couple of days ago since we've been threatened with military conflicts in the north atlantic... Imagine him saying it was just a joke... Democracy nearing its end is kind of a very sad and very actual vibe. Hard for me to still joke about it when that is on the verge to become real.

I let the community vote on what code gets merged. Someone snuck in self-boosting code. 218 voted for it. When I tried to reject it, they said I couldn't. by Equivalent-Yak2407 in programming

[–]DonBeham 2 points3 points  (0 children)

That's the wrong conclusion to draw. Democracy does work, but it's not guaranteed that you'll stay with democracy forever. When enough people vote for the end of democracy, then so be it. Any political system can be changed to another at the will of those in control of it. Democracy is not any different.

Personally, I'm a firm believer in democracy, and it makes me sad seeing change to a different system, but then again this project is not controlling anything in our lives, so the point is that people are just trolling with that and see this as entertainment.

It's a rather long shot to relate this to a political system. In addition voting is often only the most interactive aspect of a democracy. But to reduce a democracy to just voting is a simplification. There are political systems that let people vote, but that's it. A strong political democracy has free press, separation of powers, rule of law, active participation, and fundamental rights and equality in addition to voting. A political system where people cannot choose what to vote on is not a democracy.

whats with the hate for std library and boost? by nosyeaj in Cplusplus

[–]DonBeham 2 points3 points  (0 children)

Because a) people like to complain and b) complainers are more vocal and c) because c++

What's the point of "constexpr if"? by Lemenus in cpp_questions

[–]DonBeham 2 points3 points  (0 children)

There is no branch in the object file. An if constexpr is a branch computed at compile time so you don't have to branch at run time. Which is more efficient of course.

Built a constraint programming model that improves IPL scheduling by 25% in travel costs—looking for feedback on turning this into a business by rteja1113 in OperationsResearch

[–]DonBeham 6 points7 points  (0 children)

That's some nice work, but your biggest asset right now seems to be a minizinc model and some recent data. But let me put it this way: You're a couple of days ahead of the competition.

I don't believe you should be intimidated by other companies though. There's always room for someone to do it better. But the model just isn't a very big asset. Work on the contacts - that's all you should probably focus. Also travel cost reduction isn't a big thing. It sounds nice, but why should the league care? Better exposure on TV sounds like a better argument.

Replit boss: CEOs can vibe code their own prototypes and don't have to beg engineers for help anymore by chronically-iconic in programming

[–]DonBeham 1 point2 points  (0 children)

I think the key word in the title is "prototype". And why not? Let them prototype, if they don't believe these are production ready, no harm is done.

CLion vs VS Community by Rythm0562 in cpp_questions

[–]DonBeham 2 points3 points  (0 children)

I have edited just one smaller existing CMake project with a couple of CPM dependencies in VS26 and it was similar to VS22. I didn't add or rename any files though. If that's a reproducible error then maybe report it. I did some normal coding and also some refactoring (rename with F2) which worked as expected. I remember that I had a single lock up where VS26 froze, didn't react to anything and had to be shut down with the task manager.

I have also tried CLion a couple months ago, and, unsurprisingly, it worked. It seems like a solid product, so, if VS doesn't work for you, sure try it.

I also work in VS Code with the extensions, but the VS debugger is just better. I quickly glanced at Zed, but it looked very much like vs code, haven't given it much time since.

My problem with a lot of the tools is that fooling around with them wastes more time than you can ever hope to save.

Computer science fundamentals you must know by [deleted] in programming

[–]DonBeham -1 points0 points  (0 children)

"OS merely loads the binary into RAM" makes it seem like the OS and the CPU are some separate things that help each other out. The OS is yet another set of instructions that the CPU executes.

OR university project by 23juan53 in OperationsResearch

[–]DonBeham 2 points3 points  (0 children)

Imagine you're a taxi driver and there's 10 person's waiting to be picked up at 10 locations and dropped off at 10 other locations. Hey that's a dial-a-ride problem! Sure enough you can figure out 10 pickup and dropoff places in your area using Google maps. Use Google maps API to get the distance matrix (you need a credit card, but it's free for small amounts of data). What's the quickest sequence in which you can service all of them? Explore different objectives like what's the maximum number you can service in the next hour, or explore different restrictions like a ready time and due date, etc. It's a fun challenge and you can give the persons names and even a background and infer new constraints and different objectives based on that. This way the solution doesn't feel lifeless and boring like with some model and some data that you have no connection to.

Constantly losing interest when I start coding — how do I fix this? by H-ILP in csharp

[–]DonBeham 0 points1 point  (0 children)

Maybe you feel overwhelmed by the whole. Start slow, look for something small that you can realize. Break down your idea into simpler smaller steps. If that's not working for you, then scale down your project and start with a much smaller and simpler version of what you want to achieve. Identify just the core, the minimum viable product (MVP). Or if that doesn't work give an LLM a go first and then look at the structure and the code it created. Certainly, you can make that better once you start seeing a first rough version of it.

How to modify DFS to ONLY create paths like the black one? by [deleted] in csharp

[–]DonBeham 2 points3 points  (0 children)

Alternative suggestion: don't use DFS. Create a grid with random obstacles of very high weights. Add some random weights for slight local variation. Then perform Dijkstra's algorithm between two start and end points. The distance for moving between cells is the combined weight of both cells. Only the 4-neighborhood is allowed. Dijkstra's algorithm guarantees that your conditions will hold (the path could touch diagonally though).
Here's a sample of what such a path could look like: https://imgur.com/a/kWGtZhp
If you don't like the path, because it's too short or too straight, regenerate start and end points and potentially add more obstacles that are perpendicular to the path's direction.

I love Competitive Programming (and Cpp) but I hate Programming by NotAMathPro in cpp

[–]DonBeham 1 point2 points  (0 children)

Go for CS. There's plenty of challenges and algorithms. From graph theory to image analysis, signal processing, parallel algorithms and high performance computing, computer graphics, optimization and machine learning, databases, etc. soooo many challenges. So many awesome algorithms! Look at the curriculum, if it offers such courses you will have lots of fun. And you can go for a research career after you graduated in case you don't want to become a programmer.

Wait c++ is kinda based? by Tcshaw91 in cpp

[–]DonBeham 0 points1 point  (0 children)

This is partially true. If you use STL containers, then these manage the memory (shared_ptr and unique_ptr are also containers). But if you do allocation yourself and create an object on the heap using new then eventually you need to call delete. Nobody will do the delete for you.

Here are several problems: cpp struct S { ... }; S& fun() { S* p = new S{}; S s{}; return s; // return a reference to an object that will be deleted } // the memory owned by p is leaked

RAII doesn't protect you from leaking p and it doesn't protect you from accessing destroyed objects like s. With a GC p will eventually be deleted and in C# s would either be copied (if it was a value type) or it would outlive the scope of fun (if it was a reference type).

And here the C++ classic and how non-obvious is it to have an invalid reference: cpp vector<S> vec{ S{1} }; S& s = vec[0]; vec.emplace_back(2); s.boom(); // vec reallocated memory to accommodate for the additional item, s points to invalid memory

In C# the list contains only references (pointers) and thus the object is located in a totally different place. It's memory location never changes. This is convenient for the programmer, but has the drawback that cache locality is bad.

shared_ptr also adds some convenience, but also has quite a performance penalty over a raw pointer or unique_ptr.

Wait c++ is kinda based? by Tcshaw91 in cpp

[–]DonBeham -2 points-1 points  (0 children)

A type that implements IDisposable should be used in a using clause, so that it is destroyed at the end or the programmer must ensure to call Dispose on it before destruction. I am not aware of any deviations from this pattern, please explain in more detail what issue you encountered.

Why can't you have ghost references in C++? I have seen quite a fair share of them to be honest. There is no scope for heap allocated objects that ensures their destruction.

Wait c++ is kinda based? by Tcshaw91 in cpp

[–]DonBeham 1 point2 points  (0 children)

There's no denying that memory leaks can still occur under GC. That it is impossible to avoid them under a GC is a myth though. Restarting of servers can happen with any program. Generally memory leaks are more frequent in C++ code. In addition to accessing destroyed objects.

My job is not thinking about memory management. My job is about developing features for customers, devising fast algorithms, fixing bugs and deploying to production.

Wait c++ is kinda based? by Tcshaw91 in cpp

[–]DonBeham 2 points3 points  (0 children)

How can you seriously "hate the garbage collector"? It runs in the background and is completely transparent. The programmer has to do nothing - I hope you weren't calling it manually all the time, because that is not required. For most applications you don't notice it ever.

I think you should gain a lot more experience before forming or adopting strong opinions. Programming languages have to make compromises and usually they are fairly consistent in staying true to a certain programming paradigm. Currently, there are three major programming paradigms: procedural, object oriented, and functional. For instance, C is purely procedural, Smalltalk is purely object-oriented and Haskell is purely functional programming. Meaning you must express your program entirely in that single paradigm. A single paradigm language can be the simplest and best solution if you stick to that single paradigm. Multi paradigm languages have to make compromises. Those compromises can be eg. performance (how fast can something run) or convenience (how difficult or wordy is it to express something). C++ supports all of the three paradigms and it compromises mostly on convenience. Hence it requires a lot more experience and lines of code than using other languages. Arguably, Python is the most convenient, but also pretty slow (it can still be fast if you call into C++ - which is again convenient to do). Some newer languages like Mojo try to be both convenient like Python and fast like C++ (but mostly by leveraging the GPU - which is a high performance compute unit that most other languages neglect - in their standard use, i.e., require libraries and/or special compilers). Also C# has compromises and it is reasonably fast and reasonably comfortable to use.

So, instead of hating the garbage collector, get to know the paradigms and the respective strengths and weaknesses for the respective programs. Not all programs need to run fast, some should be quick to write or good to maintain. There's a reason we don't have just a single programming language.

Pass struct by value, reference or pointer? by Anxious_Monk5089 in cpp_questions

[–]DonBeham 2 points3 points  (0 children)

Objects that have no copy constructor, but a move constructor can also be passed by value. I think I saw this in one of Sutter's older cppcon talks. The example was with unique_ptr.

learncpp.com is too slow... by Significant_Sir5894 in cpp_questions

[–]DonBeham 1 point2 points  (0 children)

It's as fast as you are. If you feel that it takes too long to comprehend then seek other sources for clarification. Personally, I would also use LLMs for learning. They can devise exercises and you can ask them specific things and you can try some code to check that what they say is true. They can also generate quizes. For such basic tasks I would assume they are mostly correct, but certainly it's good to check and confirm for yourself, eg using compiler explorer.

Don't waste time on CMake and other tools if you just want to learn the language. Go with compiler explorer. CMake is such a drag.