C++ tip thread

C++ tip thread
Let's learn from each other

Attached: 1500994968210.jpg (1469x1102, 320K)

Other urls found in this thread:

en.cppreference.com/w/cpp/language/lambda
godbolt.org/g/USuu47
twitter.com/NSFWRedditImage

Use rust instead

Why?

Reminder that std::move doesn't move; it's only purpose is to cast an lvalue into an rvalue and then you move it, ideally using std::swap

What are l and rvalues?

in layman's terms, an lvalue is a value that already exists, eg: int a = 0; or std::string name; and rvalues are temporaries, eg: int&& sum = 7 + 9;
std::string&& temp = "OP"+string(" is a fag");

there are several reasons one would argue as why, and I do agree to some of them. In the end it comes down to preference IMO. Just use whatever you feel like using

I'd better learn how to develop and design the soft so I won't be shitting myself hard when writing something. I mean, I know the syntax well (enough), but if it comes for the real task I suck dicks because I can't convert my thoughts into the piece of code.

Fuck this.

When using vectors, do you still need to account for the \0 character like C?

I'd say that _R_values are the stuff after the assignment operator, the _right_ (i.e. temporary stuff) and _L_values are the ones on the left, generally a symbol that exists somewhere and has an address you can write to or read from

No, you don't.
If you want to iterate through a vector using an index, your loop condition will look like this:
for(std::size_t i = 0; i < yourvector.size(); ++i)
{
//write your logic here
}

But modern C++ gives you a better idiom; range for:
for(const auto& element : yourvectorname)
{
//do whatever with 'element'
}

But if you want to modify the index (for whatever reason), you'd use the first version and you don't have to look for the null character '\0'

yeah, but I wanted to simplify it, even if my explanation was really dumbed down.

how can i use a array of function pointers in a class ?

First question: Why?
Second question: Why not use lambdas instead?

Okay, here's my C++ tip: Use C instead.

t. C hacker

Just want to write a different version of a Chip8 emulator, instead of writing huge switch cases a Jump table seems easier.
Never heard of lambdas

pic related

Attached: rust shill hd.png (2955x2785, 785K)

>never heard of lambdas
Today's your day then:
en.cppreference.com/w/cpp/language/lambda

I don't get it. Is the joke that c++ is bad because code in pic doesn't work. Or is the joke that the person switching to rust is stupid?

>EXIT_SUCCESS
Holy shit, I never knew this was a thing. You learn something new everyday.

Attached: 1528034615504.png (960x923, 1.18M)

Lurk moar newfag.

Not really new but sure. Expecting the code in the pic to work is stupid but I wouldn't be suprised if people did.

I agree. The main advantage rust has is that in big projects, there really is a chance of fucking something up. Rust has the advantage of the compiler complaining over every little thing, so memory errors are far less common (except when fuckers use unsafe). I don't know whose idea it was to promote Rust by bashing C++'s memory management, but there are a ton of other reasons to use Rust, like the package management, cargo, pattern matching, etc

>std::unique_ptr

you can use memcpy to effectively reinitialize an array

Attached: 6767676767.jpg (680x490, 73K)

no you can’t you fucking liar.

There is absolutely no need to use class destructors. If your program needs one, then you are doing something wrong

What is raw C pointers?

you want a std::vector of std::function's

garbage in garbage out

Obsolete

your bait is shit m8

Don't use too many templates. Don't instantiate existing templates (like the ones from the STL) with too many different class arguments, don't have too many classes that use templates at all.
There's a reason modern programming languages don't use templates, but generics: Compile time.
The massive amount of generated code that has to be compiled due to templated code is the reason modern C++ codebases take hours to build, even on build servers.

Except for absolutely performance critical work, just use std::variant. It's more than fast enough and you will thank yourself if a full recompile takes 4 minutes instead of four hours when you program finally become a huge abomination.

Note that in embedded programming, using function pointers over lambdas may be beneficial because code that deals with lambdas has a significant overhead, since lambdas may capture state.

>lambdas have overhead because they might capture state
>lambdas
>overhead
godbolt.org/g/USuu47

Not until you rewrite all C libraries and OS interfaces in C++ with equal performance.

That's optimised away, tard. Try a nontrivial lamda that also captures heap state.