Friendly reminder that you should return reference to an object iff:

Friendly reminder that you should return reference to an object iff:
1. The object itself is passed to the scope as a non-const reference
2. If the object is created within the function scope, it is static
3. In no other situation

Reminder that you don't also need multi-return functions, create the expected values first and pass them as non-cost references into the function. These are called out params.

Other urls found in this thread:

learncpp.com/cpp-tutorial/15-2-rvalue-references/
learncpp.com/cpp-tutorial/15-3-move-constructors-and-move-assignment/
isocpp.org/blog/2016/02/a-bit-of-background-for-the-unified-call-proposal
twitter.com/NSFWRedditImage

no shit sherlock

But user, you should return reference to an object iff:
1. The object itself is passed to the scope as a non-const reference
2. If the object is created within the function scope, it is static
3. In no other situation

Reminder that you don't also need multi-return functions, create the expected values first and pass them as non-cost references into the function. These are called out params.

i did read your post before replying
now continue dumping your animegirl folder pls

Yes but you should return reference to an object iff:
1. The object itself is passed to the scope as a non-const reference
2. If the object is created within the function scope, it is static
3. In no other situation

Reminder that you don't also need multi-return functions, create the expected values first and pass them as non-cost references into the function. These are called out params.

yeah that's the stuff

Now tell me all bout r-value references because that shit is confusing and I understand nothing.

Despite having the word “value” in their names, l-values and r-values are actually not properties of values, but rather, properties of expressions.

Every expression in C++ has two properties: a type (which is used for type checking), and a value category (which is used for certain kinds of syntax checking, such as whether the result of the expression can be assigned to). In C++03 and earlier, l-values and r-values were the only two value categories available.

The actual definition of which expressions are l-values and which are r-values is surprisingly complicated, so we’ll take a simplified view of the subject that will largely suffice for our purposes.

It’s simplest to think of an l-value (also called a locator value) as a function or an object (or an expression that evaluates to a function or object). All l-values have assigned memory addresses.

When l-values were originally defined, they were defined as “values that are suitable to be on the left-hand side of an assignment expression”. However, later, the const keyword was added to the language, and l-values were split into two sub-categories: modifiable l-values, which can be changed, and non-modifiable l-values, which are const.

It’s simplest to think of an r-value as “everything that is not an l-value”. This notably includes literals (e.g. 5), temporary values (e.g. x+1), and anonymous objects (e.g. Fraction(5, 2)). r-values are typically evaluated for their values, have expression scope, and cannot be assigned to. This non-assignment rule makes sense, because assigning a value applies a side-effect to the object. Since r-values have expression scope, if we were to assign a value to an r-value, then the r-value would either go out of scope before we had a chance to use the assigned value in the next expression (which makes the assignment useless) or we’d have to use a variable with a side effect applied more than once in an expression.

C++11 adds a new type of reference called an r-value reference. An r-value reference is a reference that is designed to be initialized with an r-value (only). While an l-value reference is created using a single ampersand, an r-value reference is created using a double ampersand:

int x = 5;
int &lref = x; // l-value reference initialized with l-value x
int &&rref = 5; // r-value reference initialized with r-value 5

R-value references have two properties that are useful. First, r-value references extend the lifespan of the object they are initialized with to the lifespan of the r-value reference. l-value references to const objects can do this too, but it’s far more useful for r-value references since r-values have expression scope otherwise. Second, non-const r-value references allow you to modify the r-value!
R-value references are more often used as function parameters. This is most useful for function overloads when you want to have different behavior for l-value and r-value arguments.

void fun(const int &lref) // l-value arguments will select this function
{
std::cout

learncpp.com/cpp-tutorial/15-2-rvalue-references/
Why are you dumping learncpp articles? If anyone wanted to know this they can just find it out for themselves.

But user! You fail to understand thatyou should return reference to an object iff:
1. The object itself is passed to the scope as a non-const reference
2. If the object is created within the function scope, it is static
3. In no other situation

Reminder that you don't also need multi-return functions, create the expected values first and pass them as non-cost references into the function. These are called out params.

Yeah I know the unique IPs didn't increase, I should have whipped out my phone to make that comment like you did when that "other user" asked about r-values.

But anyway user, you didn't answer my question: why are you dumping learncpp articles? If anyone wanted to know this they can just find it out for themselves.

You see the problem is, you should return reference to an object iff:
1. The object itself is passed to the scope as a non-const reference
2. If the object is created within the function scope, it is static
3. In no other situation

Reminder that you don't also need multi-return functions, create the expected values first and pass them as non-cost references into the function. These are called out params.

Quality. but I'm still wondering why are you dumping learncpp articles? If anyone wanted to know this they can just find it out for themselves.

Because you should return reference to an object iff:
1. The object itself is passed to the scope as a non-const reference
2. If the object is created within the function scope, it is static
3. In no other situation

Reminder that you don't also need multi-return functions, create the expected values first and pass them as non-cost references into the function. These are called out params.

You could at least try posting the other articles that are on there instead of being some kind of spamming faggot, it's really not a good look for you user.
Here, try this one: learncpp.com/cpp-tutorial/15-3-move-constructors-and-move-assignment/
But I'm still wondering why are you dumping learncpp articles? If anyone wanted to know this they can just find it out for themselves.

Very nice, user.

I am certainly not going to outdo this explanation, but perhaps I can add a tl;dr that summarizes, yet slightly simplifies the key points.

tl;dr: An R-value reference is a reference to an object that is going to die as soon as you are done looking at it.

Why is that useful? Because, if you were going to make a COPY of that object, you can save processing time by MOVING the data instead. The object is going to die anyway, so you might as well steal the data from underneath it. What point is there copying a bunch of data and then throwing away the original, amirite?

So let's say my function --a constructor is a typical example of such a function-- is going to make a copy of a large vector. In old-timey C++, I would take a const reference to the vector as a parameter, then copy it.

In C++11, I can make an optimized version of that function that takes an R-value reference instead. This is a *non-const* reference to a vector that is going to be destroyed immediately after this function returns. So to save CPU time, I can move the buffer inside that vector to my own vector (my own vector being the one that I would otherwise copy it into), and point the argument vector buffer to something empty instead.

When my function returns, the vector that is going to be destroyed now points to something empty. The data the vector pointed to has been stolen by my function. Never did I need to actually copy over all the data, completely pointlessly.

If you want a move constructor and move assignment that do moves, you’ll need to write them yourself.

That's a nice explanation, user.

kys degenerate trannies

By the way you should return reference to an object iff:
1. The object itself is passed to the scope as a non-const reference
2. If the object is created within the function scope, it is static
3. In no other situation

Reminder that you don't also need multi-return functions, create the expected values first and pass them as non-cost references into the function. These are called out params.

The hormone imbalance from taking estrogen has caused your brain to atrophy.

>Friendly reminder that you should return reference to an object iff:
>1. The object itself is passed to the scope as a non-const reference
>2. If the object is created within the function scope, it is static
>3. In no other situation

Technically, this does not cover member functions returning references to member variables, which is certainly legitimate. Especially for overloaded operators.

based

>object oriented programming
off yourself

How is that related to OOP?

Member functions have a hidden this pointer passed into them

yeah i dislike anime too

Like I said before spamming isn't a good look, I mostly meant that a polite way of saying it's actually a rule violation, I guess posting anime will probably give you a pass though, but you're still a dumb animeposter replying to the wrong person lmao

Yes. But OP's clause 1 only allows references, not pointers.

You are passing this reference, aren't you?

No, this is a pointer, not a reference.

Unless the idea behind OP's rules is that pointers are a type of reference and included in the rules. That would make some form of sense.

As I said, you should return reference to an object iff:
1. The object itself is passed to the scope as a non-const reference
2. If the object is created within the function scope, it is static
3. In no other situation

Reminder that you don't also need multi-return functions, create the expected values first and pass them as non-cost references into the function. These are called out params.

Why are weebs allowed? If any other kind of poster pulled this shit they'd be banned lickety-split

Post an example.

anime website

There's a difference between hating anime and hating obnoxious weeb faggots though retard

GR 13 you cock dumpster.

>Do not use avatars or attach signatures to your posts.

At least trip up so we can filter you faggot.

Why do we even have references when we can just use pointers for everything?

Similar to pass by address, values returned by reference must be variables (you can not return a reference to a literal or an expression). When a variable is returned by reference, a reference to the variable is passed back to the caller. The caller can then use this reference to continue modifying the variable, which can be useful at times. Return by reference is also fast, which can be useful when returning structs and classes.

However, just like return by address, you should not return local variables by reference. Consider the following example:

1
2
3
4
5

int& doubleValue(int x)
{
int value = x * 2;
return value; // return a reference to value here
} // value is destroyed here

In the above program, the program is returning a reference to a value that will be destroyed when the function returns. This would mean the caller receives a reference to garbage. Fortunately, your compiler will probably give you a warning or error if you try to do this.

Return by reference is typically used to return arguments passed by reference to the function back to the caller. When to use return by reference:

When returning a reference parameter
When returning an element from an array that was passed into the function
When returning a large struct or class that will not be destroyed at the end of the function (e.g. one that was passed in)

When not to use return by reference:

Friendly reminder:
PAEDOPHILES WITH THEIR COLLECTION OF YOUNG CARTOON GIRLS ARE NOT ALLOWED HERE

Do you know what avatarfagging means you fucking reddit spill?

what does return reference to an object mean

Thats nice.

Drop the fucking avatar.

There are two main reasons for references over pointers, and they both tl;dr to syntactic sugar.

One reason is that certain operators such as [] should return something you can assign to. Having that operator return a pointer rather than a reference would mean that arrays behave syntactically different from, say, vectors. And that would suck.

The other reason is that it allows functions to take const reference parameters to avoid needless copying, again without this optimization making a syntactic difference for the caller. This allows std::string::operator+= to look just like += for integers without unnecessary slowdowns.

Even moot hated you faggots

> le plebbit meme XD XD XD XD

Perhaps you should go learn what avatarfagging is, because you're doing it right now jimmy.

Thank you.

Because passing pointers is less efficient

Now now, it took you 5 minutes to realize your mistake but you learned something new today :)

It isn't, it does the exact same thing a pointer would at low level.

ctd

When returning variables that were declared inside the function (use return by value)
When returning a built-in array or pointer value (use return by address)

Mixing return references and values

Although a function may return a value or a reference, the caller may or may not assign the result to a value or reference accordingly.
int returnByValue()
{
return 5;
}

int& returnByReference()
{
static int x = 5; // static ensures x isn't destroyed when it goes out of scope
return x;
}

int main()
{
int value = returnByReference(); // case A -- ok, treated as return by value
int &ref = returnByValue(); // case B -- compile error since the value is an r-value, and an r-value can't bind to a non-const reference
const int &cref = returnByValue(); // case C -- ok, the lifetime of the return value is extended to the lifetime of cref
}

In case A, we’re assigning a reference return value to a non-reference variable. Because value isn’t a reference, the return value is copied into value, as if returnByReference() had returned by value.

In case B, we’re trying to initialize reference ref with the copy of the return value returned by returnByValue(). However, because the value being returned doesn’t have an address (it’s an r-value), this will cause a compile error.

In case C, we’re trying to initialize const reference ref with the copy of the return value returned by returnByValue(). Because const references can bind to r-values, there’s no problem here. Normally, r-values expire at the end of the expression in which they are created -- however, when bound to a const reference, the lifetime of the r-value (in this case, the return value of the function) is extended to match the lifetime of the reference (in this case, cref)

...

Dereferencing pointer is just as expensive and it does nothing. Remember that pointers are just an int value, however your OS has to look for the underlying data structure or function pointed by the pointer

There's a big difference between people who don't like anime and people who complain about "ANIMU OTAKU KAWAII BAKA" faggots fyi

No one in this thread is speaking weebshit
Now quickly fuck off back to 9gag

I don't understand. Using a reference compiles into a pointer dereference in asm is what I meant.

Since when did you only have to "speak" weebshit? This is an imageboard after all, and images speak a thousand words.

Learn the difference, it may save you from looking like a faggot.

You're dumb. Lurk 2 years before posting.

Well then either put up or shut up, I suggest you to get out :)

When you pass a pointer to a function by address, the pointer’s value (the address it points to) is copied from the argument to the function’s parameter. In other words, it’s passed by value! If you change the function parameter’s value, you are only changing a copy. Consequently, the original pointer argument will not be changed.
#include

void setToNull(int *tempPtr)
{
// we're making tempPtr point at something else, not changing the value that tempPtr points to.
tempPtr = nullptr; // use 0 instead if not C++11
}

int main()
{
// First we set ptr to the address of five, which means *ptr = 5
int five = 5;
int *ptr = &five;

// This will print 5
std::cout

>easily triggered, let alone triggered by anime
Please, re-read Also tell me, if this was some retard spamming pics of their favorite children cartoon instead of generic anime pics, wouldn't you admit this shit should be taken down?
But oh wait, it's anime pics, and weebs have to stick together of course! Can't let them get punished for acting like faggots like demonstrated!

This is a common point of confusion, so let’s clarify:

When passing an argument by address, the function parameter variable receives a copy of the address from the argument. At this point, the function parameter and the argument both point to the same value.
If the function parameter is then dereferenced to change the value being pointed to, that will impact the value the argument is pointing to, since both the function parameter and argument are pointing to the same value!
If the function parameter is assigned a different address, that will not impact the argument, since the function parameter is a copy, and changing the copy won’t impact the original. After changing the function parameter’s address, the function parameter and argument will point to different values, so dereferencing the parameter and changing the value will no longer affect the value pointed to by the argument.

...

I still don't understand, why are still you in this thread? I never realized why do sjws get so buttflustered by anime and frogs, really

>resorting to SJW shit
Come on now user, this isn't Sup Forums. I was actually enjoying this little debate too...

see
Just because you're choosing to be retarded, does not mean you are not actually retarded.

Go learn what avatarfagging means in the first place, newfag

Yeah I know how pointers work. I was saying that using references is not more efficient than using pointers. When you pass by reference, at low level it is still actually passing a pointer. It generates the same code as passing a pointer would. It is purely a language difference.

>If any other kind of poster pulled this shit they'd be banned lickety-split
This actually explains why this site has been filled with so many awful 200+ reply threads lately, maybe it's the mods idea of getting rid of the pondscum but it's still given these faggots a really simple way to make actual shitty threads.
Do I have to use an anime pic to make this post get taken seriously too?

This

References are implemented in pointers

Ranges in C++ WHEN?
Modules in C++ WHEN?
UFCS in C++ WHEN?

They fucked up the UFCS
isocpp.org/blog/2016/02/a-bit-of-background-for-the-unified-call-proposal

>598 year old vampire

>It's real
lmao

Just realized
>6+2chan /tech/
>calling anyone who doesn't agree with them an SJW
What a surprise!

2bh it's people like this that make others think lesser of anime posters, this is exactly the kind of person you think of when you think "faggot weeaboo", it's really brining the anime-fan cause down

I wonder how they will implement modules. Won't this fuck up backwards compatibility?

I guess they can't ever implement modules.

>I'M A PAEDOPHILE! LOOK AT ME! A PEDO! I LIKE TO POST YOUNG CARTOON GIRLS! WOW! I'M A PEDO!
Take this.

They could have learned UFCS from D or Nim or even Rust

honestly lads should I kys myself soon?

Wrong thread, onii chan

In my eyes this was actually the perfect thread
Also sick dubs user-chu

it is funny that the article in that image frames Sup Forums as a cohesive entity capable of collectively caring about image enough to have stopped using pedobear because of backlash

brat a shit