The as-if rule

来自cppreference.com
< cpp‎ | language

 
 
C++语言

|- class="t-nv-h1" | colspan="5" | 一般主题 |- | |- |

预处理器
注释

|

|-

|- class="t-nv-h1" | colspan="5" | 流程控制 |-

条件执行语句

| |- |

|

|-

循环语句

| |- |

|

|-

跳转语句

| |- |

|

|-

|- class="t-nv-h1" | colspan="5" | 函数 |-

函数声明 Lambda函数声明 inline说明符 异常说明符 (已弃用) noexcept说明符 (C++11)

|- class="t-nv-h1" | colspan="5" | Exceptions |- | |- |

|

|-

|- class="t-nv-h1" | colspan="5" | 命名空间 |- | |- |

|

|-

|- class="t-nv-h1" | colspan="5" | 类型 |- | |- |

|

|-

说明符

| |- |

decltype (C++11)
auto (C++11)
alignas (C++11)

|

|-

存储持续期说明符 Initialization

| |- |

|

|-

|- class="t-nv-h1" | colspan="5" | 表达式 |- | |- |

值类别
求值顺序

|

|-

替代表示 字面量 布尔型 - 整型 - 浮点型 字符型 - 字符串 - nullptr (C++11) 用户定义 (C++11)

|- class="t-nv-h1" | colspan="5" | Utilities |-

属性 (C++11) Types typedef声明 类型别名声明 (C++11) Casts 隐式类型转换 - 显示类型转换 static_cast - dynamic_cast const_cast - reinterpret_cast 内存分配

| |- |

|

|-

|- class="t-nv-h1" | colspan="5" | 类 |- | |- |

|

|-

类特有的函数属性

| |- |

|

explicit (C++11)
static

|-

静态成员函数

| |- |

|

|- |- class="t-nv-h1" | colspan="5" | 模板 |- | |- |

|

|- |- class="t-nv-h1" | colspan="5" | 杂项 |- | |- |

|

|-

 
允许任何和所有的代码转换,不改变观察程序的行为
原文:
Allows any and all code transformations that do not change the observable behavior of the program
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

目录

[编辑] 解释

C + +编译器允许进行任何修改了程序,只要下面仍是如此
原文:
The C++ compiler is permitted to perform any changes to the program as long as the following remains true:
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

[编辑] 注释

因为编译器(通常情况下)无法进行分析,以确定它是否或不执行I / O或挥发性访问外部库的代码,第三方库的调用,通过优化也不会受到影响。然而,标准的库调用可能被其他调用所取代,淘汰,或在优化过程中添加到程序中.
原文:
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.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
程序与不确定的行为,例如由于访问一个数组越界,修改const对象,求值顺序违规行为等,都是免费的作为,如果规则:他们经常更换不同的优化设置重新编译时,观察到的行为。例如,如果有符号整数溢出测试依赖于该溢出的结果,例如if(n+1 < n) abort();it is removed entirely by some compilers因为签署溢出是不确定的行为和优化是免费的,假设它永远不会发生,检查是多余的.
原文:
Programs with undefined behavior, e.g. due to access to an array out of bounds, modification of a const object, 求值顺序 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 签署溢出是不确定的行为 and the optimizer is free to assume it never happens and the test is redundant.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
复制省略是唯一定义的例外,因为如果规则.
原文:
复制省略 is the only well-defined exception from the as-if rule.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

[编辑] 示例

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;
}

输出:

# 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;

[编辑] 另请参阅

来自“http://zh.cppreference.com/mwiki/index.php?title=cpp/language/as_if&oldid=38185