Namensräume
Varianten
Aktionen

The as-if rule

Aus cppreference.com
< cpp‎ | language

 
 
Sprache C + +
Allgemeine Themen
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Flusskontrolle
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Bedingte Ausführung Aussagen
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Iterationsanweisungen
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Gehe Aussagen
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Funktionen
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Funktion Erklärung
Lambda-Funktion Erklärung
Funktions-Template
inline-Spezifizierer
Exception-Spezifikationen (veraltet)
noexcept Spezifizierer (C++11)
Ausnahmen
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Namespaces
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Types
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
Specifiers
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
cv Planer
Lagerdauer Planer
constexpr Spezifizierer (C++11)
auto Spezifizierer (C++11)
alignas Spezifizierer (C++11)
Initialisierung
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Literale
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Expressions
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
alternative Darstellungen
Utilities
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Types
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
Typ Aliasdeklaration (C++11)
Attribute (C++11)
Wirft
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
impliziten Konvertierungen
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-Stil und funktionale Besetzung
Speicherzuweisung
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Classes
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Class-spezifische Funktion Eigenschaften
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
explizit (C++11)
statisch
Besondere Member-Funktionen
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Templates
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Klassen-Template
Funktions-Template
Template-Spezialisierung
Parameter Packs (C++11)
Verschiedenes
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Inline Montage
 
Ermöglicht sämtliche Code-Transformationen, die sich nicht ändern, das beobachtbare Verhalten des Programms
Original:
Allows any and all code transformations that do not change the observable behavior of the program
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Inhaltsverzeichnis

[Bearbeiten] Erklärung

Die C + +-Compiler ist es gestattet, irgendwelche Änderungen an dem Programm so lange durchführen, wie die folgende bleibt wahr:
Original:
The C++ compiler is permitted to perform any changes to the program as long as the following remains true:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • Zugriffe (liest und schreibt) an der flüchtigen Objekte treten in der gleichen Reihenfolge wie geschrieben .
    Original:
    Accesses (reads and writes) to the volatile objects occur in the same order as written.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Bei Beendigung des Programms ist die zu schreibenden Daten auf alle Dateien genau so, wie wenn das Programm ausgeführt wurde, wie geschrieben .
    Original:
    At program termination, the data written to all files is exactly as if the program was executed as written.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Alle Ein-und Ausgabe-Operationen auftreten, in der gleichen Reihenfolge und mit dem gleichen Inhalt wie wenn das Programm ausgeführt wurde, wie geschrieben .
    Original:
    All input and output operations occur in the same order and with the same content as if the program was executed as written.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

[Bearbeiten] Notes

Da der Compiler ist (in der Regel) nicht in der Lage, den Code einer externen Bibliothek zu bestimmen, ob es tut oder nicht ausführen I / O oder flüchtige Zugang zu analysieren, nennt Drittanbieter-Bibliothek sind auch nicht durch die Optimierung beeinflusst. Jedoch können Standard-Bibliothek Anrufe durch andere Anrufe ersetzt werden, beseitigt oder hinzugefügt, um das Programm während der Optimierung .
Original:
Because the compiler is (usually) unable to analyze the code of an external library to determine whether it does or does not perform I/O or volatile access, third-party library calls also aren't affected by optimization. However, standard library calls may be replaced by other calls, eliminated, or added to the program during optimization.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Programme mit undefinierten Verhalten, z. B. aufgrund der zu einem Array außerhalb der Grenzen, Modifikation einer const-Objekt zugreifen, sind Auswertungsreihenfolge Verletzungen, etc, frei von der Als-ob-Regel: Sie wechseln oft beobachtbare Verhalten, wenn sie mit unterschiedlichen Optimierungseinstellungen neu kompiliert. Zum Beispiel, wenn ein Test für Ganzzahl Überlauf beruht auf dem Ergebnis dieser Überlauf, zB if(n+1 < n) abort();, it is removed entirely by some compilers weil vorzeichenbehafteter Überlauf undefiniert ist Verhalten und der Optimierer frei annehmen, dass es nie passiert, und der Test ist überflüssig .
Original:
Programs with undefined behavior, e.g. due to access to an array out of bounds, modification of a const object, Auswertungsreihenfolge violations, etc, are free from the as-if rule: they often change observable behavior when recompiled with different optimization settings. For example, if a test for signed integer overflow relies on the result of that overflow, e.g. if(n+1 < n) abort();, it is removed entirely by some compilers because vorzeichenbehafteter Überlauf undefiniert ist Verhalten and the optimizer is free to assume it never happens and the test is redundant.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Kopieren elision ist die einzige wohldefinierte Ausnahme von der Als-ob-Regel .
Original:
Kopieren elision is the only well-defined exception from the as-if rule.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[Bearbeiten] Beispiel

int& preinc(int& n) { return ++n; }
int add(int n, int m) { return n+m; }
 
// volatile input to prevent constant folding
volatile int input = 7;
 
// volatile output to make the result a visible side-effect
volatile int result;
 
int main()
{
    int n = input;
// using built-in operators would invoke undefined behavior
//    int m = ++n + ++n;
// but using functions makes sure the code executes as-if 
// the functions were not overlapped
    int m = add(preinc(n), preinc(n));
    result = m;
}

Output:

# full code of the main() function as produced by the GCC compiler
# x86 (Intel) platform:
        movl    input(%rip), %eax   # eax = input
        leal    3(%rax,%rax), %eax  # eax = 3 + eax + eax
        movl    %eax, result(%rip)  # result = eax
        xorl    %eax, %eax          # eax = 0 (the return value of main())
        ret
 
# PowerPC (IBM) platform:
        lwz 9,LC..1(2)
        li 3,0          # r3 = 0 (the return value of main())
        lwz 11,0(9)     # r11 = input;
        slwi 11,11,1    # r11 = r11 << 1;
        addi 0,11,3     # r0 = r11 + 3;
        stw 0,4(9)      # result = r0;
        blr
 
# Sparc (Sun) platform:
        sethi   %hi(result), %g2
        sethi   %hi(input), %g1
        mov     0, %o0                 # o0 = 0 (the return value of main)
        ld      [%g1+%lo(input)], %g1  # g1 = input
        add     %g1, %g1, %g1          # g1 = g1 + g1
        add     %g1, 3, %g1            # g1 = 3 + g1
        st      %g1, [%g2+%lo(result)] # result = g1
        jmp     %o7+8
        nop
 
# in all cases, the side effects of preinc() were eliminated, and the
# entire main() function was reduced to the equivalent of result = 2*input + 3;

[Bearbeiten] Siehe auch