I'm just going to add a detailed reference for the as-if rule and the volatile keyword. (At the bottom of these pages, follow the "see also" and "References" to trace back to the original specs, but I find cppreference.com much easier to read/understand.)
Particularly, I want you to read this section
volatile object - an object whose type is volatile-qualified, or a subobject of a volatile object, or a mutable subobject of a const-volatile object. Every access (read or write operation, member function call, etc.) made through a glvalue expression of volatile-qualified type is treated as a visible side-effect for the purposes of optimization (that is, within a single thread of execution, volatile accesses cannot be optimized out or reordered with another visible side effect that is sequenced-before or sequenced-after the volatile access. This makes volatile objects suitable for communication with a signal handler, but not with another thread of execution, see std::memory_order). Any attempt to refer to a volatile object through a non-volatile glvalue (e.g. through a reference or pointer to non-volatile type) results in undefined behavior.
So the volatile keyword specifically is about disabling the compiler optimization on glvalues. The only thing here the volatile keyword can affect is possibly return x
, the compiler can do whatever it wants with the rest of the function.
How much the compiler can optimize the return depends on how much the compiler is allowed to optimize the access of x in this case (since it isn't reordering anything, and strictly speaking, isn't removing the return expression. There is the access, but it is reading and writing to the stack, which is should be able to streamline.) So as I read it, this is a grey area in how much the compiler is allowed to optimize, and can easily be argued both ways.
Side note: In these cases, always assume the compiler will do the opposite of what you wanted/needed. You should either disable optimization (at least for this module), or try to find a more defined behavior for what you want. (This is also why unit testing is so important) If you believe it is a defect, you should bring it up with the developers of C++.
This all is still really hard to read, so trying to include what I think is relevant so that you can read it yourself.
glvalue A glvalue expression is either lvalue or xvalue.
Properties:
A glvalue may be implicitly converted to a prvalue with
lvalue-to-rvalue, array-to-pointer, or function-to-pointer implicit
conversion. A glvalue may be polymorphic: the dynamic type of the
object it identifies is not necessarily the static type of the
expression. A glvalue can have incomplete type, where permitted by the
expression.
xvalue The following expressions are xvalue expressions:
a function call or an overloaded operator expression, whose return
type is rvalue reference to object, such as std::move(x); a[n], the
built-in subscript expression, where one operand is an array rvalue ;
a.m, the member of object expression, where a is an rvalue and m is a
non-static data member of non-reference type; a.*mp, the pointer to
member of object expression, where a is an rvalue and mp is a pointer
to data member; a ? b : c, the ternary conditional expression for some
b and c (see definition for detail); a cast expression to rvalue
reference to object type, such as static_cast(x); any
expression that designates a temporary object, after temporary
materialization. (since C++17) Properties:
Same as rvalue (below). Same as glvalue (below). In particular, like
all rvalues, xvalues bind to rvalue references, and like all glvalues,
xvalues may be polymorphic, and non-class xvalues may be cv-qualified.
lvalue The following expressions are lvalue expressions:
the name of a variable, a function, or a data member, regardless of
type, such as std::cin or std::endl. Even if the variable's type is
rvalue reference, the expression consisting of its name is an lvalue
expression; a function call or an overloaded operator expression,
whose return type is lvalue reference, such as std::getline(std::cin,
str), std::cout << 1, str1 = str2, or ++it; a = b, a += b, a %= b, and
all other built-in assignment and compound assignment expressions;
++a and --a, the built-in pre-increment and pre-decrement expressions;
*p, the built-in indirection expression; a[n] and p[n], the built-in subscript expressions, except where a is an array rvalue (since
C++11); a.m, the member of object expression, except where m is a
member enumerator or a non-static member function, or where a is an
rvalue and m is a non-static data member of non-reference type; p->m,
the built-in member of pointer expression, except where m is a member
enumerator or a non-static member function; a.*mp, the pointer to
member of object expression, where a is an lvalue and mp is a pointer
to data member; p->*mp, the built-in pointer to member of pointer
expression, where mp is a pointer to data member; a, b, the built-in
comma expression, where b is an lvalue; a ? b : c, the ternary
conditional expression for some b and c (e.g., when both are lvalues
of the same type, but see definition for detail); a string literal,
such as "Hello, world!"; a cast expression to lvalue reference type,
such as static_cast(x); a function call or an overloaded
operator expression, whose return type is rvalue reference to
function; a cast expression to rvalue reference to function type, such
as static_cast(x). (since C++11) Properties:
Same as glvalue (below). Address of an lvalue may be taken: &++i1
and &std::endl are valid expressions. A modifiable lvalue may be used
as the left-hand operand of the built-in assignment and compound
assignment operators. An lvalue may be used to initialize an lvalue
reference; this associates a new name with the object identified by
the expression.
as-if rule
The C++ compiler is permitted to perform any changes to the program as long as the following remains true:
1) At every sequence point, the values of all volatile objects are stable (previous evaluations are complete, new evaluations not started)
(until C++11)
1) Accesses (reads and writes) to volatile objects occur strictly according to the semantics of the expressions in which they occur. In particular, they are not reordered with respect to other volatile accesses on the same thread.
(since C++11)
2) At program termination, data written to files is exactly as if the program was executed as written.
3) Prompting text which is sent to interactive devices will be shown before the program waits for input.
4) If the ISO C pragma #pragma STDC FENV_ACCESS is supported and is set to ON, the changes to the floating-point environment (floating-point exceptions and rounding modes) are guaranteed to be observed by the floating-point arithmetic operators and function calls as if executed as written, except that
the result of any floating-point expression other than cast and assignment may have range and precision of a floating-point type different from the type of the expression (see FLT_EVAL_METHOD)
notwithstanding the above, intermediate results of any floating-point expression may be calculated as if to infinite range and precision (unless #pragma STDC FP_CONTRACT is OFF)
If you want to read the specs, I believe these are the ones you need to read
References
C11 standard (ISO/IEC 9899:2011):
6.7.3 Type qualifiers (p: 121-123)
C99 standard (ISO/IEC 9899:1999):
6.7.3 Type qualifiers (p: 108-110)
C89/C90 standard (ISO/IEC 9899:1990):
3.5.3 Type qualifiers
xor eax, eax; ret
forfn()
(which contains the volatile variable), or not? Is it allowed by the standard, or not? It is a yes-no question. If you think that it is allowed, then please write an answer, because the current most upvoted answer tells otherwise, as far as I understand it. – Laughry