2

Reference to a literal is possible only if the reference is declared as constant.

But why is a pointer to a const object not possible in case of literals? i.e.

int const& ref = 5;//

But why is the same feature not available for a pointer, pointing to a constant object?

  • 2
    because literals are not stored in memory so there is nothing to point to – ratchet freak Jan 17 '14 at 9:51
  • @ratchetfreak "literals are not stored in memory" where are they stored then? – Kolyunya Jan 17 '14 at 11:04
  • @Kolyunya depends on the literal/compiler implementation, word sized literals are typically in the opcode where they are used, string literals are stored in memory but the pointer to the first char is again in the opcode – ratchet freak Jan 17 '14 at 11:12
  • @ ratchet freak I think literals are also stored in memory.But there is no way to get the address(of literals other than strings), which is needed for a pointer.But the language implementation permits accessing address of string literal by char const* str = "string". – sk patra Jan 17 '14 at 12:11
  • 1
    Small nit: 5 is a literal, ref is a variable, albeit one that doesn't vary. – Blrfl Jan 17 '14 at 13:41
7

In the C++ view of the world, a literal does not occupy any memory. A literal just exists.

This view makes that there is no address for a pointer to refer to when it would point to a literal and for that reason, pointers to literals are forbidden.

Const references are actually the exception here in that they allow apparent indirect access to a literal. What happens underneath is that the compiler creates a temporary object for the reference to refer to and that temporary object is initialized with the value of the reference.

This creating of a temporary that the reference gets bound to is only allowed for const references (and gets used in other scenarios as well, not only for literals), because that is the only case where a reference/pointer to a temporary won't have undesired side-effects.

  • This creating of a temporary that the reference gets bound to is only allowed for const references (and gets used in other scenarios as well, not only for literals), because that is the only case where a reference/pointer to a temporary won't have undesired side-effects. whats if int const * ptr = adress; is this has any side effect?@ Bart van Ingen Schenau – sk patra Jan 17 '14 at 12:05
  • 1
    @skpatra: How do you propose to get a value for address, given that it can't know it will be used to initialize a int const* and &42 has no meaning in the C++ view of the world. – Bart van Ingen Schenau Jan 17 '14 at 13:00
4

In C++, literals exist only in the compiler. They are used in expressions, assignments and initialisations. The value of a literal may be found in the running program if it has been used to initialise a variable or temporary, but the literal itself is not.

A name that is a reference to a literal has meaning, because a reference is a value, so that wherever the name is used it can be replaced by the literal value itself. In certain special cases it is replaced by a temporary variable initialised to that value, and the required const qualifier protects that variable against being modified.

A name that is a pointer to a literal has no meaning. Pointers refer to storage locations, and a literal doesn't have one.

It would be hypothetically possible for C++ to allow pointers to literals, and to interpret them as pointing to a temporary variable initialised to that value, and presumably to protect that value with a const qualifier. The reasons it wasn't defined that way is either (a) it's not very useful (b) it would be incompatible with C. You'd have to ask Bjarne about that one.

Please note that the above applies to integers and floats. Literal strings have different rules, because in many ways they are already const pointers.

  • thank you very much. Your answer exactly explains the reason why pointer to constant variable for literals is not possible. – sk patra Jan 20 '14 at 5:51
0

In my opinion, this behavior comes from the semantical differences of pointers and references in C++.

The following code snippet with reference works fine:

const int& r = 5;
std::cout << &r << std::endl;

... since a temporary object is created when you assign the value to the const reference, and you can take the address of that temporary object.

The equivalent code snippet with pointer would be like this:

const int *p = &5;
std::cout << p << std::endl;

This one, however, does not compile, since &5, i.e. address of 5, has no meaning in C.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.