Why are pointers to literals not possible?

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?

5

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.

2

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.

1

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật