3

I looked the C++14 reference and could not see where the standard would says that the const built in types are inlined by the compiler and not allocated. i.e. the claim is

const int i = 5;
std::cout<<i;

everywhere i is used with be replaced with 5 and no memory space will be allocated. Can someone please point to the standard section ?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
babycub
  • 31
  • 2
  • 2
    What does it mean to "inline a type"?? – Kerrek SB Mar 18 '15 at 01:42
  • Data types are not inlined. Functions and methods may be inlined, depending on the compiler's optimization settings. – Thomas Matthews Mar 18 '15 at 01:42
  • Seems like you are not using the terminology correctly, can you provide a reference? Seems like you are asking when a variable is odr used like in this [question](http://stackoverflow.com/q/28506342/1708801). – Shafik Yaghmour Mar 18 '15 at 01:43
  • Try section 3.2 perhaps. – Kerrek SB Mar 18 '15 at 01:43
  • The "optimization" technique you are talking about dates way back in history to the C language. In the modern language specifications, the optimization may fall under the "as-if" rule. – Thomas Matthews Mar 18 '15 at 01:46
  • Why do you presume this is guaranteed somewhere in the Standard? Inling is allowed for any given point of usage (unless taking the address / initialising a reference etc.), but not required - up to the compiler/optimiser. – Tony Delroy Mar 18 '15 at 02:07

2 Answers2

2

Sounds like a combination of two concepts odr use and the as-if rule.

Odr-use, which would be covered in covered in section 3.2 One definition rule but we can also find some relevant section in 4.1 Lvalue-to-rvalue conversion which says:

When an lvalue-to-rvalue conversion is applied to an expression e, and either

  • e is not potentially evaluated, or
  • the evaluation of e results in the evaluation of a member ex of the set of potential results of e, and ex names a variable x that is not odr-used by ex (3.2),

the value contained in the referenced object is not accessed.

and has the following involved example which seems to show a captured by reference local variable being used outside of its lifetime but is actually not the case since it is not odr-used and therefore does not actually require the object to be allocated and thus can be optimized away.

[ Example:

struct S { int n; };
auto f() {
    S x { 1 };
    constexpr S y { 2 };
    return [&](bool b) { return (b ? y : x).n; };
}    
auto g = f();
int m = g(false); // undefined behavior due to access of x.n outside its lifetime
int n = g(true); // OK, does not access y.n

—end example ]

This comes down to the as-if rule which says the compiler only has the emulate the observable behavior of a program, basically it governs what optimizations are allowable. Even though an optimization may be allowable the compiler does not have to perform the optimization. If an object yields a constant expression and the address is not required then by the as-if rule no memory needs to be allocated for it since the effect of not allocating memory would not be observable.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
1

In most cases, the compiler will emit processor instructions containing the constant value and not allocate a read/write memory location. But this depends on the compiler settings.

The compiler may elect to place the constants into a read-only section of memory. It may place the constant into the executable.

Print out the assembly language listing of the function to see the truth.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154