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

11 Eylül 2016 Pazar

Statement Order

Giriş
Açıklaması şöyle. Niyeyse bu konuda tam bir uyuşma sağlanamamış.
14 Every value computation and side effect associated with a full-expression is sequenced before every value computation and side effect associated with the next full-expression to be evaluated.
Elimizde şöyle bir kod olsun.
using Clock = std::chrono::high_resolution_clock;

auto t1 = Clock::now(); // Statement 1
foo();                  // Statement 2
auto t2 = Clock::now(); // Statement 3

auto elapsedTime = t2 - t1;
Bu kodun sırasının değişmemesi gerekir. Eğer şu hale gelirse kod bozulur
using Clock=std::chrono::high_resolution_clock;

foo();                  // Statement 2
auto t1 = Clock::now(); // Statement 1
auto t2 = Clock::now(); // Statement 3

auto elapsedTime = t2 - t1;
as-if kuralı
Kod şöyle olsun
int fred(int x)
{
  auto t1 = std::chrono::high_resolution_clock::now();
  int y = foo(x);
  auto t2 = std::chrono::high_resolution_clock::now();
  return y;
}
t1 ve t2 değişkenleri kullanılmıyor. Bu durumda kod içindeki now() metodları teorik olarak yer değiştirebiliyor.

Buna as-if kuralı deniyor. Yani gözlemlenebilen davranışta değişiklik olmaması. Tabi bu kuralı uygulamak ve ispatlamak derleyici açısında zor. Özellikle sistem çağrılarının yan etkisinin olmadığını bilmesi imkansız gibi.