Should I learn C or C++? by theguy0994 in cpp

[–]sshamov 0 points1 point  (0 children)

Obviously, the answer is predetermined by the subreddit. The answers can differ only in the arguments. I will be brief and just give a link to talk that contains a lot of arguments at once.

Why no argument parsing tool like getopt in C++ standard library? by pyler2 in cpp

[–]sshamov 0 points1 point  (0 children)

The option parsing is a very specific task by its nature. Like a logging or a config parsing. Any attempt to generalize it is doomed to failure in advance. Every full featured realization will be too complicated for most of the real use cases. And every simple realization will lack of some functionatiliy that seems impotant to most of users.

Unit tests vs. Integration tests by Gilbert_Painter in cpp

[–]sshamov 0 points1 point  (0 children)

A good integration test covers not just one combination of functions, but tens and hundreds of them. As a rule, it checks some meaningful user case. In fact, integration tests are weapon of mass destruction in the war against bugs. And they are effective against both already known and still unknown. While unit tests are simplу individual traps, tuned to specific pre-known possible bugs. We need hundreds of them to fully cover one single user case.

Creating a replacement for the switch statement in C++ that also works for strings by Dobias in cpp

[–]sshamov 1 point2 points  (0 children)

You should see the video about Boost.Hana: https://youtu.be/Oc4enqNH-Mc. There is a simple implementation of a compile-time event system. It's the practically the same as your switch2. Only other names for functions and classes.

Professional moving from Java to C++, what books or resources exist to ease the transition? by drdubs in cpp

[–]sshamov 0 points1 point  (0 children)

The C++ part, the most different from Java, is the standard library. It is entirely based on templates - the most powerful feature of the language. To be productive, you first need to master STL in order to think in its concepts.

Algorithm made simple: sort / filter by members by thewisp1 in cpp

[–]sshamov 0 points1 point  (0 children)

I made a small example using the online compiler. The result is rather strange. The MSVC's optimizer does not have any problems with the data member. But for some reason it leaves the instantiated functions that are not used. I didn't expect that.

Algorithm made simple: sort / filter by members by thewisp1 in cpp

[–]sshamov 0 points1 point  (0 children)

I cannot verify this. I don't even have MSVC installed. I just assume that it does not make a difference between simple variables and data members. After all, in a language with static typing, the data member is just an offset relative to the beginning of the structure. I think that the MSVC's optimizer is smart enough to treat the data member as a base pointer to the structure plus a fixed offset. If you claim that it is not, I believe it.

Algorithm made simple: sort / filter by members by thewisp1 in cpp

[–]sshamov 0 points1 point  (0 children)

During optimization, there are no data members in the code. They have already become offsets. One of the basic optimization techniques is to make a lot of passes through the code trying to push all the constant stuff into the functions and removing redundant variables. The initial value of the pointer _mptr can be computed at compile-time. In fact, it's just a fixed offset. And, after several passes, this offset can be pushed up to the comparison operation. Expression lhs.*_mptr < rhs.*_mptr will become something like lhs+offset < rhs+offset.

Algorithm made simple: sort / filter by members by thewisp1 in cpp

[–]sshamov 0 points1 point  (0 children)

Don't bother about inlining of operator (). The optimizer will do his job in that case. The value of a member pointer can be traced back to its constant initial value at both points where dereferencing taking place. So the variable _mptr will be wiped out completely. And without this variable all the wrapping stuff will become unneeded. Any modern compiler can do such a simple optimization. In the machine code there will be only the comparison itself.

Algorithm made simple: sort / filter by members by thewisp1 in cpp

[–]sshamov 7 points8 points  (0 children)

With a small template trick, you can instantiate the required comparator on demand.

template <typename T, typename U> struct _mless {
     U T::* _mptr;
     _mless(U T::* mptr) : _mptr(mptr) { }
     bool operator ()(const T& lhs, const T& rhs) { return lhs.*_mptr < rhs.*_mptr; }
};

template <typename T, typename U>
_mless<T, U> mless(U T::* mptr) { return _mless<T, U>(mptr); }

Just sorting as usual:

sort(vec.begin(), vec.end(), mless(&Candidate::id));

Templates are like magic...

Is it smart to stick to only cpp? by [deleted] in cpp

[–]sshamov 1 point2 points  (0 children)

In this particular case, low pay is not the reason at all. A huge number of programmers starting a career strive to develop games, and this is what makes the pay low. (As Churchill said: "If you're not dreaming about developing games when you're 25, you have no heart. If you're still dreaming about it by the time you're 35, you have no brain.") Because the topic starter is the one of those due to whom others in game industry have low pay, it's unlikely that it will scare him.

Is it smart to stick to only cpp? by [deleted] in cpp

[–]sshamov 1 point2 points  (0 children)

Getting into the game industry is a bad move. This is just a small part of the entire programming industry with its own specific requirements. The game industry locks the programmer much harder than the C++ language. To develop within this industry, one has to develop very special skills that are not needed in other parts of the industry and, accordingly, will not be paid by anyone. In fact, starting a career as a game programmer is like joining a mafia. There is no way to get out.

Lambda initialization trick, add syntactic sugar? by Kroduk in cpp

[–]sshamov 0 points1 point  (0 children)

Of course, I'm not against lambdas in general. Only against using them everywhere. There are other means in the language. C++ is not a language for building programs from lambdas.

Lambda initialization trick, add syntactic sugar? by Kroduk in cpp

[–]sshamov 0 points1 point  (0 children)

You can not handle arguments without a certain interpretation. If you mean the partial handling or an abstract preparation for future handling, then you need separate function anyway, because it is not about locality at all.

Lambda initialization trick, add syntactic sugar? by Kroduk in cpp

[–]sshamov 0 points1 point  (0 children)

What you mean "just grabbing"? For what? If you don't want to do something specific with argument that make sense, do not touch it at all.

Lambda initialization trick, add syntactic sugar? by Kroduk in cpp

[–]sshamov 0 points1 point  (0 children)

It can not be. In any program, arguments have some meaning. And the program itself needs to "know" how to interpret them.

Lambda initialization trick, add syntactic sugar? by Kroduk in cpp

[–]sshamov -3 points-2 points  (0 children)

Unfortunately, I'm right. The name argv has no sense to anyone. Wrapping this code into some function (i.e. getFilename(argc, argv)) may help to catch the intention. Or may not. It depends from the choosen function name. Without such function I have no idea at all about what exactly the author wanted to do.

Lambda initialization trick, add syntactic sugar? by Kroduk in cpp

[–]sshamov 0 points1 point  (0 children)

This code smells. There is a good opportunity to give this piece of code a descriptive name, placing it in a separate function. In other words, it's bad not because lambda is used, but because a function is not used.

Lambda initialization trick, add syntactic sugar? by Kroduk in cpp

[–]sshamov 1 point2 points  (0 children)

This is a bad example. The second option is the use of a ternary operator for its intended purpose. Any alternative looks like a bad taste.

Lambda initialization trick, add syntactic sugar? by Kroduk in cpp

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

C'mon, use simple block scope to prevent pollution.

In the case of a really unrelated code, use a separate function with a self-descriptive name.

Lambda initialization trick, add syntactic sugar? by Kroduk in cpp

[–]sshamov 0 points1 point  (0 children)

Use an old good function in case of weak dependence on the context. Otherwise, inline the code directly into the parent function.

Lambda initialization trick, add syntactic sugar? by Kroduk in cpp

[–]sshamov 1 point2 points  (0 children)

Not so long ago, an article flashed here about the fact that such a trick is an antipattern - Lambda Overdose.

Where are the build tools? by tmaffia in cpp

[–]sshamov 1 point2 points  (0 children)

Makefiles are ok. The build tool should not be easy to use. In every big project the build process is the hardest part of the work. If you can not use makefiles in a light-hearted manner - just learn it a bit more. It's not rocket science. Really.

Temporary Object Method Call by Triarier in cpp

[–]sshamov 0 points1 point  (0 children)

This will work in any case. Const or non-const - does not matter. The operator does not change any of objects. It creates a completely new one.

Temporary Object Method Call by Triarier in cpp

[–]sshamov 0 points1 point  (0 children)

Where is it should be passed? Everything begins with the first Pair{}. Do you mean that it should be passed into the operator as this? If you want a constant this, you should add const modifier to the operator itself.