cv_qualifier in declarator grammar section

X

xxx

I am having difficulty seeing why cv_qualifier, which is used as a type
specifier in the declaration section of the ISO/IEC grammar, is defined in
the declarator section. A declaration can consist of specifiers followed by
declarators. Because of the cv_qualifier, I am now confused as to what
defines a declarator. Can anyone help clear this up for me? Thank you!
 
V

Victor Bazarov

xxx said:
I am having difficulty seeing why cv_qualifier, which is used as a type
specifier in the declaration section of the ISO/IEC grammar, is defined in
the declarator section. A declaration can consist of specifiers followed by
declarators. Because of the cv_qualifier, I am now confused as to what
defines a declarator. Can anyone help clear this up for me? Thank you!

Without getting formal (I am not good at it), I think the reason for it to
be there is that you could write

const int a;

and

int const a;

, and they'd mean the same thing.

Victor
 
X

xxx

// cv_qualifier as a specifier when to the left of "x"
int const x = 123;
const int x = 123;

// cv_qualifier as a declarator when to the right of "x"
int x (123) const;

Do you mean that everything left to the identifier is part of the type and
everything to the right is part of the declarator?
 
V

Victor Bazarov

xxx said:
// cv_qualifier as a specifier when to the left of "x"
int const x = 123;
const int x = 123;

// cv_qualifier as a declarator when to the right of "x"
int x (123) const;

I don't think this is valid.
Do you mean that everything left to the identifier is part of the type and
everything to the right is part of the declarator?

No, I don't mean that. What problem are you trying to solve, anyway?

V
 
N

Nicolas Pavlidis

Victor said:
Without getting formal (I am not good at it), I think the reason for it to
be there is that you could write

const int a;

and

int const a;

, and they'd mean the same thing.

Am I right to think that these two statements are the same if it's an
non pointertype?

What I mean:
const int *my_pointer = NULL;
and
int const *my_pointer are different things.

Am I right here?

Kind regards,
Nicolas
 
V

Victor Bazarov

Nicolas said:
Am I right to think that these two statements are the same if it's an
non pointertype?

What I mean:
const int *my_pointer = NULL;
and
int const *my_pointer are different things.

Am I right here?

They mean exactly the same. 'my_pointer' is a pointer to a constant
integer.

The source of often seen confusion is the asterisk. E.g.

const int * p;

and

int * const p;

are _not_ the same. And another example involving this typedef

typedef int * pint;

where declarations

const int * p;

and

const pint p;

are _not_ the same. Reading declarations correctly takes a bit of getting
used to but it's not that difficult.

Victor
 
N

Nicolas Pavlidis

Victor said:
They mean exactly the same. 'my_pointer' is a pointer to a constant
integer.

Ah, ok.
The source of often seen confusion is the asterisk. E.g.

const int * p;

and

int * const p;

are _not_ the same. And another example involving this typedef

typedef int * pint;

where declarations

const int * p;

and

const pint p;

are _not_ the same. Reading declarations correctly takes a bit of
getting used to but it's not that difficult.

Ok, now it's clear to me, thank you!

Kind regards,
Nicolas
 
X

xxx

Victor Bazarov said:
I don't think this is valid.


No, I don't mean that. What problem are you trying to solve, anyway?

V

I should have verified it with MS VC++ 7.1. The "int x (123) const" passed
with flying colors with GNU C++ 3.2.3.

Designing a C++ based interpreter is what I'm doing so I would like to make
sure I understand things before I jump right into implementation.

I am trying to understand why cv_qualifier is not a specifier. The previous
syntax may be invalid for declaring constant int, but you could still
declare "void someclass::func (int value) const" where the const in this
case is exactly a declarator because it is part of the member's definition
(as in a member function that doesn't modify any data of the class). I
understand that it is because of this that the cv is a cv_qualifier rather
than a cv_specifier because it can appear in two different context. Ah, I
think I just answered my own question!

Thank you for helping me realize this =)
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top