What is a literal?

S

Stefan Ram

I used to say that a literal is defined by the programming
language while a name is defined by the user. For example,
the value of »5« is defined by C++, while the value of »i«
in »constexpr int i = 3;« is defined by the user.

With user-defined literals, however, this is no longer true.
The value of a literal such as »5m« can now be defined by
the user.

So what is a literal, then?

One might try to say that the value of a literal can be
determined from its characters in a systematic manner, for
example, 417, 418, 419, 420, 421 versus names such as ijk,
ijl, ijm, ijn, but the values of the literals »true« and
»false« cannot be derived from their characters by a rule.

One might say that names start with name-start characters,
such as letters, while literals start with other characters,
such as digits, but the literal »R"(abc)"« starts with a
character that could start a name as well.

So, can we give a general meaningful definition for the
meaning of the word »literal« other than somewhat
tautological wordings like »a literal is everything that
ISO 14882:2011 defines to be a literal«?
 
V

Victor Bazarov

I used to say that a literal is defined by the programming
language while a name is defined by the user. For example,
the value of »5« is defined by C++, while the value of »i«
in »constexpr int i = 3;« is defined by the user.

With user-defined literals, however, this is no longer true.
The value of a literal such as »5m« can now be defined by
the user.

So what is a literal, then?

One might try to say that the value of a literal can be
determined from its characters in a systematic manner, for
example, 417, 418, 419, 420, 421 versus names such as ijk,
ijl, ijm, ijn, but the values of the literals »true« and
»false« cannot be derived from their characters by a rule.

Those are keywords, remember?
One might say that names start with name-start characters,
such as letters, while literals start with other characters,
such as digits, but the literal »R"(abc)"« starts with a
character that could start a name as well.

There is a very limited set of prefixes that are allowed to form string
literals, and 'R' is just one of them. What's exactly your objection to
the form R"something" as a literal? It's not like it can be
misinterpreted...
So, can we give a general meaningful definition for the
meaning of the word »literal« other than somewhat
tautological wordings like »a literal is everything that
ISO 14882:2011 defines to be a literal«?

Section 2.14 of the Standard explains it.

We're way beyond the point at which an identifier is defined as "a token
that starts with a letter or an underscore". It's more complex. If you
want to write it down, you'd have to quote some portion of the Standard
(at least part of its Grammar appendix).

What's exactly the point of trying to simplify (or dare I say,
trivialize) the language? Does it look less daunting when you can say
that "anything that starts with a number is a literal"? Can you say
"training wheels"? Well, you will need to abandon them at some point,
it might be better to do it sooner rather than later.

V
 
M

Martin Shobe

I used to say that a literal is defined by the programming
language while a name is defined by the user. For example,
the value of »5« is defined by C++, while the value of »i«
in »constexpr int i = 3;« is defined by the user.

With user-defined literals, however, this is no longer true.
The value of a literal such as »5m« can now be defined by
the user.

So what is a literal, then?

A literal is a section of the source that denotes a value. It doesn't
matter that you had to tell it how to map the source to the value
elsewhere in your code.
One might try to say that the value of a literal can be
determined from its characters in a systematic manner, for
example, 417, 418, 419, 420, 421 versus names such as ijk,
ijl, ijm, ijn, but the values of the literals »true« and
»false« cannot be derived from their characters by a rule.

Sure it can, simply use the rule
"true" -> 1
"false" -> 0
One might say that names start with name-start characters,
such as letters, while literals start with other characters,
such as digits, but the literal »R"(abc)"« starts with a
character that could start a name as well.

So, can we give a general meaningful definition for the
meaning of the word »literal« other than somewhat
tautological wordings like »a literal is everything that
ISO 14882:2011 defines to be a literal«?

See above. (Though each language does get to decide what sections of
source denotes values, so you don't get away from that entirely.)

Martin Shobe
 
A

Alf P. Steinbach

I used to say that a literal is defined by the programming
language while a name is defined by the user. For example,
the value of »5« is defined by C++, while the value of »i«
in »constexpr int i = 3;« is defined by the user.

With user-defined literals, however, this is no longer true.
The value of a literal such as »5m« can now be defined by
the user.

So what is a literal, then?

Wikipedia's explanation is not bad, IMHO:

"In computer science, a literal is a notation for representing a fixed
value in source code. Almost all programming languages have notations
for atomic values such as integers, floating-point numbers, strings, and
booleans; some also have notations for elements of enumerated types and
compound values such as arrays, records, and objects.

In contrast to literals, variables or constants are symbols that can
take on one of a class of fixed values, the constant being constrained
not to change. Literals are often used to initialize variables"


Cheers & hth.,

- Alf
 
S

Stefan Ram

Alf P. Steinbach said:
Wikipedia's explanation is not bad, IMHO:
"In computer science, a literal is a notation for representing a fixed
value in source code.

Does this exclude »(2+3)« as a literal?
 
A

Alf P. Steinbach

Does this exclude »(2+3)« as a literal?

Maybe Wikipedia's text is too vague to say that for sure.

But as I see it for the general CS meaning (and certainly in the sense
of the C++ standard) it's not a literal, it's an expression involving 2
literals, even if that expression is evaluated at compile time.

Dang it gets tricky this. :)


Cheers,

- Alf
 
M

Martin Shobe

Does this exclude »(2+3)« as a literal?

That would depend on the semantics of the language. A language certainly
could define "(2+3)" to be a literal. (The value it denotes wouldn't
even have to be 5.) But, in most commonly used languages, "(2+3)" would
not be a literal even though we know which value it evaluates to. Only
the "2" and the "3" would be literals.

Martin Shobe
 
S

Stefan Ram

Alf P. Steinbach said:
Maybe Wikipedia's text is too vague to say that for sure.

And maybe C++'s literals do not necessarily represent fixed values:

#include <iostream>
#include <ostream>
#include <cstdlib>

int operator "" _r( unsigned long long d ){ return ::std::rand(); }
int main(){ if( 0_r - 0_r )::std::cout << "not fixed" << std::endl; }

This code might be »bad style«, but I think the language
specification still calls »0_r« a »literal«.
 
A

Alf P. Steinbach

And maybe C++'s literals do not necessarily represent fixed values:

#include <iostream>
#include <ostream>
#include <cstdlib>

int operator "" _r( unsigned long long d ){ return ::std::rand(); }
int main(){ if( 0_r - 0_r )::std::cout << "not fixed" << std::endl; }

He he :).

How about, making C++11 user-defined literals a special exception from
the general rule?


Cheers,

- Alf
 
S

Stefan Ram

This code might be »bad style«

#include <iostream>
#include <ostream>
#include <cstdlib>

int & operator "" _
( unsigned long long i ){ static int a[ 10 ]; return a[ i ]; }

int main()
{ 0_ = 2; 1_ = 3; ::std::cout << 0_ << ", " << 1_ << '\n';
0_ = 5; 1_ = 7; ::std::cout << 0_ << ", " << 1_ << '\n'; }
 
V

Victor Bazarov

This code might be »bad style«

#include <iostream>
#include <ostream>
#include <cstdlib>

int & operator "" _
( unsigned long long i ){ static int a[ 10 ]; return a[ i ]; }

int main()
{ 0_ = 2; 1_ = 3; ::std::cout << 0_ << ", " << 1_ << '\n';
0_ = 5; 1_ = 7; ::std::cout << 0_ << ", " << 1_ << '\n'; }

Just to prevent a possible error, I'd change the ending of line 6 to

return a[ i % 10 ]; }

V
 
M

Martin Shobe

This code might be »bad style«

#include <iostream>
#include <ostream>
#include <cstdlib>

int & operator "" _
( unsigned long long i ){ static int a[ 10 ]; return a[ i ]; }

int main()
{ 0_ = 2; 1_ = 3; ::std::cout << 0_ << ", " << 1_ << '\n';
0_ = 5; 1_ = 7; ::std::cout << 0_ << ", " << 1_ << '\n'; }

I guess this shows that not all of c++'s user-defined literals are
literals.

Martin Shobe
 
A

Alf P. Steinbach

This code might be »bad style«

#include <iostream>
#include <ostream>
#include <cstdlib>

int & operator "" _
( unsigned long long i ){ static int a[ 10 ]; return a[ i ]; }

int main()
{ 0_ = 2; 1_ = 3; ::std::cout << 0_ << ", " << 1_ << '\n';
0_ = 5; 1_ = 7; ::std::cout << 0_ << ", " << 1_ << '\n'; }

I guess this shows that not all of c++'s user-defined literals are
literals.

Oh they're literals all right -- in C++. I didn't think about that
first, but yes that's a good point. One just has to accommodate this,
and, if feeling sufficiently energetic, update Wikipedia (not me!).


Cheers,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,742
Latest member
AshliMayer

Latest Threads

Top