rvalue etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
rvalue etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

5 Mart 2019 Salı

rvalue Nedir

rvalue Değişkenin Adresi
Normalde rvalue değişkenin adresiyle ilgilenmemek gerekir.

Örnek
Aynı adres tekrar tekrar rvalue değişken için kullanılabilir.
Örnek
Elimizde şöyle bir kod olsun
const int *funct(const int &x) { return &x; }
Şöyle yaparız
// same address
std::cout << funct(3) << std::endl; //#1
std::cout << funct(4) << std::endl; //#2
Ancak aynı ifade içinde derleyici mevcuren farklı adresler kullanacaktır. Şöyle yaparız
// different addresses
std::cout << funct(3) << std::endl << funct(4) << std::endl; //#3
Açıklaması şöyle
Two alive objects in C++ (almost) always have different addresses.

Since temporaries in #1 #2 have non-overlapping lifetimes, the compiler is free to reuse the storage of #1 for #2 .

But in #3 all temporaries are alive until the end of the expression (for obvious reasons) and in this case they have to have different addresses.
rvalue ve & operator
Açıklaması şöyle. Adresle ilgilenmememiz için bize zorluk bile çıkarılmış :)
Now, there's an expression category rule that you can't apply & to an expression of the rvalue category. The purpose of this rule and these categories is to avoid errors where a temporary object is used after it is destroyed. 
Örnek
Şu kod derlenmez.
int *p = &5;    // not allowed due to category rules
*p = 6;         // oops, dangling pointer
Ancak rvalue değişkenin adresini almak mümkün. Şöyle yaparız.
template<typename T> auto f(T&&t) -> T& { return t; }
// ...
int *p = f(5); // Allowed
*p = 6;        // Oops, dangling pointer, no compiler error message.
rvalue Değişken lvalue Haline Gelebilir
Örnek
Şöyle yaparız.
int arr[] = {1, 2};
int* p = &arr[0];
*(p + 1) = 10;   // OK: p + 1 is an rvalue, but *(p + 1) is an lvalue

14 Kasım 2017 Salı

Function Ref Qualifier

Function Ref Qualifier C++11 ile gelen ve metodun sağ tarafında eklenen & işaretidir. Eğer nesne rvalue ise farklı bir metodun çağrılması sağlanabilir.
// t.cpp
#include <iostream>

struct test{
  void f() &{ std::cout << "lvalue object\n"; } //l-value ref qualified
  void f() &&{ std::cout << "rvalue object\n"; } //r-value ref qualified
};

int main(){
  test t;
  t.f(); // lvalue
  test().f(); // rvalue
}
Bu özelliği şimdiye kadar hiç kullanmadım.

Function Ref Qualifier ve R-Value Farkı
Bu örnekte r-value kullanılır.
#include <string>

void foo(const std::string &) {}
void foo(std::string &&) = delete;

int main()
{
  std::string world = "World";
  foo("Hello");   // Doesn't compile, wants to use foo(std::string &&)
  foo(world);     // Compiles
}