Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are literals not const (except strings)?

Literals are, in general, prvalues. Strings are a special case, defined as an array of char (meaning lvalue). This question is specifically about not string literals.

Why are non-string literals not const?

foo(42); // passes foo an `int`, not a `const int`.

You can't modify a non-string literal, so doesn't it make sense for it to be const?

like image 873
Ivan Rubinson Avatar asked Aug 29 '18 12:08

Ivan Rubinson


People also ask

Are string literals const?

String constants, also known as string literals, are a special type of constants which store fixed sequences of characters. A string literal is a sequence of any number of characters surrounded by double quotes: "This is a string."

What is the difference between literal and string?

Definition. String literal in Java is a set of characters that is created by enclosing them inside a pair of double quotes. In contrast, String Object is a Java is a set of characters that is created using the new() operator. Thus, this explains the main difference between string literal and string object.

Are literal constants?

Constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals. Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are enumeration constants as well.

Can a string be a literal?

A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string.


2 Answers

The literal answer is probably because literals predate the addition of const to the language, so naturally they are not const.

But the practical answer is - const prvalues are fundamentally strange things. You cannot create them from any of the fundamental types, but you can have a const prvalue of class type. But... why? Typically, we make things const to prevent further modifications right. But if it's a prvalue, it's not even a thing with identity - who is going to be there to observe its unintended modification? const prvalues prevent moving - because they're const, so you can't move from them, so its a premature pessimization.

Note that the one thing that could go wrong, that a hypothetical const literal would prevent, is already explicitly forbidden by the language:

void foo(int&);
foo(42); // error

But rather than making 42 const, the language made lvalue references non-const not allowed to bind to rvalues.

like image 115
Barry Avatar answered Sep 30 '22 13:09

Barry


Remember that by default C++ is pass by value, meaning values are copied.

There's no way to modify a numeric literal value like 42, because all you have is a copy in a variable. The literal value itself doesn't even have to be stored in memory, the compiler could use it directly in the generated code.

like image 26
Some programmer dude Avatar answered Sep 30 '22 15:09

Some programmer dude