21 Ağustos 2020 Cuma

[[noreturn]] Attribute

Giriş
Bir çok programlama dilinde noreturn anlamına gelen anahtar kelimler var. Açıklaması şöyle
Many languages have a type for this, for example never in TypeScript, Nothing in Scala and Kotlin, or Void in Haskell.

C has a _Noreturn function specifier keyword.
gcc için açıklaması şöyle
If a function f is called where f was previously declared with the noreturn attribute and f eventually returns, the behavior is undefined. [ Note: The function may terminate by throwing an exception. —end note ]

[ Note: Implementations are encouraged to issue a warning if a function marked [[noreturn]] might return. —end note ]
Bu özellik ile işaretli metoddan çıkılmamalı. Sonsuz döngü kurulabilir, abort(), exit(), throw vs kullanılabilir.

Örnek
Açıklama için örnek şöyle
[[ noreturn ]] void f() {
  throw "error"; // OK
}
[[ noreturn ]] void q(int i) {
  // behavior is undefined if called with an argument <= 0
  if (i > 0)
    throw "positive";
}
Örnek
Şöyle yaparız.
[[noreturn]] void fun() 
{
  throw std::string("Error!!!");
}

void func()
{
  fun();
}

void aTempFunc()
{
  try
  {
    func();
  }
  catch (std::string &e)
  {
    std::cout << e << std::endl;
  }
}
C Dili - _Noreturn
Aynı özellik C dilinde de var. Şöyle yaparız.
_Noreturn void f () {
    abort(); // ok
}
_Noreturn void g (int i) {  // causes undefined behavior if i<=0
    if (i > 0) abort();
}
C Dili - noreturn
_Noreturn yerine şöyle de yapılabilir.
#include <stdio.h>
#include <stdnoreturn.h>

noreturn void func()
{
  ...
}
Bu Özellik Niçin Var
Derleyicinin işini kolaylaştırmak ve optimizasyon yapmasına imkan sağlamak için var deniliyor. Ben bu özelliği hiç kullanmadım. Açıklaması şöyle
The function specifiers in C are a hint to the compiler, the degree of acceptance is implementation defined.

First of all, _Noreturn function specifier (or, noreturn, using <stdnoreturn.h>) is a hint to the compiler about a theoretical promise made by the programmer that this function will never return. Based on this promise, compiler can make certain decisions, perform some optimizations for the code generation.
Açıklaması şöyle
noreturn is a promise. You're telling the compiler, "It may or may not be obvious, but I know, based on the way I wrote the code, that this function will never return." That way, the compiler can avoid setting up the mechanisms that would allow the function to return properly. Leaving out those mechanisms might allow the compiler to generate more efficient code.
Açıklaması şöyle
This feature is of immense benefit to static analysis tools, especially ones that perform worst-case stack depth analysis. Without the noreturn attribute on a function that truly won't return, the results of such tools would be incorrect. It can also be helpful for optimizations (saving a few bytes at most, every time such a function is called) because the compiler does not have to account for the function returning; control flow within a calling function simply ceases at that point.



Hiç yorum yok:

Yorum Gönder