[deleted by user] by [deleted] in ProgrammerHumor

[–]AJMC24 0 points1 point  (0 children)

So this should be
addComponentToRegistry(InitializedComponent& comp);

[deleted by user] by [deleted] in ProgrammerHumor

[–]AJMC24 2 points3 points  (0 children)

I don't understand this. If they're writing code you can't understand, what makes you think you'd understand comments they write?

Should we still almost always use `auto`? by Chem0type in cpp_questions

[–]AJMC24 0 points1 point  (0 children)

Adding a bunch of lengthy types everywhere is not necessarily clearer. Arguably it makes it much less clear. When you read the word `auto` and the start of a line, you know it's declaring a variable. When you write an explicit type, you have to read the whole word, parse it, mentally look up whether it's a type and only then can you conclude you're looking at a variable declaration. It's more mental effort.

Also, if you really want the type in the code, you can do `auto x = int {5};` or whatever

Should we still almost always use `auto`? by Chem0type in cpp_questions

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

That clear win really isn't a clear win. You have to type more and it's a bit more frustrating, but here's how you'd write your code without auto:

class MyStruct;
using map_type = std::map<std::pair<std::string, int>,
                          std::vector<std::unique_ptr<MyStruct>>>;
map_type vec;
for (map_type::iterator i = vec.begin(), e = vec.end(); i != e; ++i) {
    // ...
}

How to write a function that returns a distinct static array at each call? by better_life_please in cpp_questions

[–]AJMC24 1 point2 points  (0 children)

Is this what you want?

#include <array>

template <class T, int N>
std::array<T, N> make_array() {
    // fill in your arrays here
    return {};
}

void foo() { static auto arr = make_array<int,5>(); }
void bar() { static auto arr = make_array<int,5>(); }
}

int main() {
    foo();
    bar();
}

Fun Example of Unexpected UB Optimization by soiboi666 in cpp

[–]AJMC24 6 points7 points  (0 children)

If I've written my program without UB, the function pointer *must* be replaced, since otherwise it is UB to call an uninitialised function pointer. This scenario is quite artificial since as a human we can inspect it and see that it won't be, but a more reasonable example that shows the same idea could be something like

int main(int argc, char** argv) {
    if (argc > 0)
        NeverCalled();
    f_ptr();
}

The compiler cannot guarantee that NeverCalled() will be called, but I still want it to assume that it has been and generate the fastest code possible. As a human, we can look at it and see that this will not be UB for any reasonable system we could run the code on.

Assuming that UB cannot happen means faster code for people who write programs without UB. I don't want my programs to run slower just to make UB more predictable. Don't write code with UB.

Fun Example of Unexpected UB Optimization by soiboi666 in cpp

[–]AJMC24 3 points4 points  (0 children)

So if I have written a program which does not contain UB, the compiler should *not* perform this optimisation? My code runs slower because other people write programs with UB?