473,695 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Are literals objects?

Hello.

I wonder if literal constants are objects, or they're only
"naked" values not contained in any object?
I have read that literal constants may not to be allocated
by the compiler. If the Standard is saying that "object is
a region of storage", I deduce from that that literal constants
aren't objects because they may not be alocated as regions of
storage in the memory.

The Standard itself doesn't tell anything if literal constants
are objects or not [at least I haven't found anything]. It
only says that literal constants are expressions.

Are my thoughts correct?

I'm asking about this because some person told me that literal
constants are objects and that the following code prooves it:

const int& x = 7;
std::cout << x;

He said that it's an object because it is possible to set a
constant reference to it. Who's right here?

--
SasQ
Apr 5 '07 #1
27 2555
Me
On Fri, 06 Apr 2007 01:06:01 +0200, Noone wrote:
Hello.

I wonder if literal constants are objects, or they're only "naked"
values not contained in any object? I have read that literal constants
may not to be allocated by the compiler. If the Standard is saying that
"object is a region of storage", I deduce from that that literal
constants aren't objects because they may not be alocated as regions of
storage in the memory.
I do not consider literal constants to be objects. Right or wrong, I
believe an object must have attributes and exhibit behavior: fields and
methods. A literal constant only has a field value and therefore has no
meaning unless it is used in a particular context.

Apr 5 '07 #2
SasQ wrote:
Hello.

I wonder if literal constants are objects, or they're only
"naked" values not contained in any object?
I have read that literal constants may not to be allocated
by the compiler. If the Standard is saying that "object is
a region of storage", I deduce from that that literal constants
aren't objects because they may not be alocated as regions of
storage in the memory.

The Standard itself doesn't tell anything if literal constants
are objects or not [at least I haven't found anything]. It
only says that literal constants are expressions.

Are my thoughts correct?
Why do you care ?
>
I'm asking about this because some person told me that literal
constants are objects and that the following code prooves it:

const int& x = 7;
That says create a temporary int from a literal 7 and bind a reference to x.
std::cout << x;
Pass the value of x to operator<<.

In this case, the compiler is quite free to allocate no storage
whatsoever for x or the int it is pointing to. I would make a wager
that some compilers would treat that code exactly the same as it would.

std::cout << int(7);
>
He said that it's an object because it is possible to set a
constant reference to it. Who's right here?
A const reference allows the compiler to create a temporary object for
any kind of type that can be constructed from a literal 7.

i.e.

struct A
{
A( int );
};

const A & a = 1;

How does this "prove" anything and why would we care ?
Apr 6 '07 #3
SasQ wrote:
Hello.

I wonder if literal constants are objects, or they're only
"naked" values not contained in any object?
If they were objects, they would have an address.

--
Ian Collins.
Apr 6 '07 #4
Ian Collins wrote:
SasQ wrote:
>Hello.

I wonder if literal constants are objects, or they're only
"naked" values not contained in any object?

If they were objects, they would have an address.
int main()
{
const int& x = 7;
return &x == 0;
}

Now, whose address I just compared to 0? Literal's? I am
not sure that's _not_ so. I wonder if there is a way to
prove it one way or the other.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 6 '07 #5
Victor Bazarov wrote:
Ian Collins wrote:
>>SasQ wrote:
>>>Hello.

I wonder if literal constants are objects, or they're only
"naked" values not contained in any object?

If they were objects, they would have an address.


int main()
{
const int& x = 7;
Doesn't that create a temporary int with a value of 7 and assign its
address to x?
return &x == 0;
}

Now, whose address I just compared to 0? Literal's? I am
not sure that's _not_ so. I wonder if there is a way to
prove it one way or the other.
Well you can't write

const int* p = &7;

and I can't think of anything else that is considered an object that
doesn't have an address.

--
Ian Collins.
Apr 6 '07 #6
Ian Collins wrote:
Victor Bazarov wrote:
>Ian Collins wrote:
>>SasQ wrote:

Hello.

I wonder if literal constants are objects, or they're only
"naked" values not contained in any object?

If they were objects, they would have an address.


int main()
{
const int& x = 7;

Doesn't that create a temporary int with a value of 7 and assign its
address to x?
Uh... Isn't compiler allowed to optimize away creation of the
temporary? <g>
>
> return &x == 0;
}

Now, whose address I just compared to 0? Literal's? I am
not sure that's _not_ so. I wonder if there is a way to
prove it one way or the other.
Well you can't write

const int* p = &7;
Well, here you're splitting hairs. The address-of operator cannot
be used on a literal _syntactically_ . It requires the expression
following the '&' to be an lvalue expression (which a literal isn't).
and I can't think of anything else that is considered an object that
doesn't have an address.
Right. By definition an object is a region of storage [that has
an address]. If the program contains '7', where is it stored? Is
it stored anywhere? If it is, doesn't that mean it has some address
(very likely contiguous) and therefore is an object (by definition)?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 6 '07 #7
Victor Bazarov wrote:
Ian Collins wrote:
>>Victor Bazarov wrote:
>>>Ian Collins wrote:

SasQ wrote:

>Hello.
>
>I wonder if literal constants are objects, or they're only
>"naked" values not contained in any object?

If they were objects, they would have an address.

Well you can't write

const int* p = &7;

Well, here you're splitting hairs. The address-of operator cannot
be used on a literal _syntactically_ . It requires the expression
following the '&' to be an lvalue expression (which a literal isn't).
So can we say if it isn't an lvalue, it isn't an object?
>>and I can't think of anything else that is considered an object that
doesn't have an address.

Right. By definition an object is a region of storage [that has
an address]. If the program contains '7', where is it stored? Is
it stored anywhere? If it is, doesn't that mean it has some address
(very likely contiguous) and therefore is an object (by definition)?
By that definition, if( a=b ) is an object because the code that
implements it lives in addressable memory!

It's very likely that an assignment from constant expression is
represented as an immediate mode instruction, so at assembly level, it
isn't addressable. On a typical machine that is, it probably is
addressable on a DS9000.

--
Ian Collins.
Apr 6 '07 #8
* Victor Bazarov:
Ian Collins wrote:
>SasQ wrote:
>>Hello.

I wonder if literal constants are objects, or they're only
"naked" values not contained in any object?
If they were objects, they would have an address.

int main()
{
const int& x = 7;
return &x == 0;
}

Now, whose address I just compared to 0? Literal's? I am
not sure that's _not_ so. I wonder if there is a way to
prove it one way or the other.
What "it" do you want to prove?

Here's the address of a literal:

#include <iostream>
void showAddress( void const* p ) { std::cout << p << std::endl; }
int main() { showAddress( "very literal string" ); }

On the other hand, the literal 7, if used in a program, might not even
exist in the machine code, because the compiler might generate machine
code that produces the same effect as using the number would have had.

Thus, some literals are objects and some are not.

And where it doesn't matter, it depends much on the compiler.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 6 '07 #9
On Apr 6, 1:06 am, SasQ <s...@go2.plwro te:
I wonder if literal constants are objects, or they're only
"naked" values not contained in any object?
I have read that literal constants may not to be allocated
by the compiler. If the Standard is saying that "object is
a region of storage", I deduce from that that literal constants
aren't objects because they may not be alocated as regions of
storage in the memory.
You're sort of right, but the issue is not really that clear.
The C++ (and the C) standard describe the results of expressions
as being lvalues or rvalues. An lvalue always refers to an
object; there's no doubt about that. An rvalue normally only
refers to an object if it has class type. But there are
contexts where an rvalue ends up being treated as an lvalue,
e.g. when it is bound to a const reference. In those cases, the
compiler is required to create an "object" to hold the value, in
order to do the binding.
The Standard itself doesn't tell anything if literal constants
are objects or not [at least I haven't found anything].
It does so indirectly. No literal constants have class type, so
a literal constant is an object if and only if it is an lvalue.
String literals are lvalues; other literals aren't.
It only says that literal constants are expressions.
Are my thoughts correct?
I'm asking about this because some person told me that literal
constants are objects and that the following code prooves it:
const int& x = 7;
std::cout << x;
He said that it's an object because it is possible to set a
constant reference to it. Who's right here?
According to the standard, the literal constant 7 is NOT an
object. The compiler creates a temporary object to hold it, and
binds the reference to this.

Note, however, that there are different ways of expressing the
same thing. The above is the way it is expressed in the
standard; other ways can be imagined.

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 6 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1262
by: Richard Hayden | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, Let's say I have a class which does not define __cmp__ or any of the rich comparison functions and let x be an instance of this class, then if I do: x < 2
11
2434
by: Kevin Buffardi | last post by:
To those experts in standard C/C++... In past I've been quick to use the string.h library for convenience, but now I'm working on a project where I'm trying to maximize portability so I want to conform to c++ standards as much as I can. Can someone point me in the right direction for using character arrays (I assume is the most convenient way) specifically with assigning values and passing/returning from functions. I appreciate it. ...
6
2014
by: kobu.selva | last post by:
I was recently part of a little debate on the issue of whether constants and string literals are considered "data objects" in C. I'm more confused now than before. I was always under the understanding that only "named" storage areas(from the standard) were data objects. Since constants and string literals don't have lvalues, they can't be data objects. Yet, I was shown the first page of chapter 2 in K&R2, which states that variables...
1
1921
by: london calling | last post by:
Hi everyone, I am developing an application which uses custom httpmodules and httphandlers to display 'portaled' content. (asp.net 1.1) The front end consists of aspx templates which contain masterzones which in turn have subzones which in turn have modules. (sound familiar!!) when a request is received an httpmodule looks at the hostname to determine which portal the request is for before working out which page is being requested and...
6
2735
by: copx | last post by:
Can you / are you supposed to free() string literals which are no longer needed? In my case I've menu construction code that looks like this: menu_items = list_new(); list_add(menu_items, "Item 1"); list_add(menu_items, "Item 2"); list_add(menu_items, "Item 3"); menu_create(menu_items, false)
8
3107
by: akameswaran | last post by:
I wrote up a quick little set of tests, I was acutally comparing ways of doing "case" behavior just to get some performance information. Now two of my test cases had almost identical results which was not at all what I expected. Ultimately I realized I don't really know how literals are treated within the interpreter. The two implementations I was looking at were: class caseFunction(object): def __init__(self):
29
2193
by: Richard Harter | last post by:
There is probably a simple way to do what I want but I don't see it. Any suggestions are welcome. Suppose I have a function foo with an argument that can be any of several types and that I want to use a union for that argument. In a header file there is the following: #define VAL_ALT union urt_value_alt .....
25
3028
by: Lennart Benschop | last post by:
Python has had the Decimal data type for some time now. The Decimal data type is ideal for financial calculations. Using this data type would be more intuitive to computer novices than float as its rounding behaviour matches more closely what humans expect. More to the point: 0.1 and 0.01 are exact in Decimal and not exact in float. Unfortunately it is not very easy to access the Decimal data type. To obtain the decimal number 12.34 one...
7
2272
by: lithiumcat | last post by:
Hi, I'm not yet very confident in my use of standard terminology, so please be kind if I'm mis-calling something, I will do my best no to make it again once pointed out. I'm wondering what is the lifetime or a compile-time string constant, I think that is what is called the storage duration of a string litteral.
0
8630
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9115
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8845
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8825
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5839
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4340
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2272
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1976
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.