order of const in function prototypes

D

Denis Remezov

johny said:
Can someone please explain to me the difference between these two:

function1( const int a)
function2( int const a)

Both seemed to compile, but what is the difference between the two above.
And why would one choose one over the other.

"const int a" and "int const a" are technically the same. In general, the
former pattern is more popular, the latter has arguably a more consistent
behaviour (some may disagree). Here is an illustration of what I mean:
Assume expression EXPR represents some type. Then EXPR const (where it is
allowed) will always have the same meaning: constant EXPR. The meaning of
const EXPR, however, will depend on what EXPR is:

typedef any_type_name EXPR;
//fine: const EXPR is the same as EXPR const

#define EXPR type_name*
//const EXPR or, directly, const type_name* now means something
different: a pointer-to-const data, not a const pointer-to-data.

By the way, a const modifier on the type of a function parameter
doesn't do anything except for preventing the function from
modifying its /own copy/ of the argument. It doesn't make sense
to use it here.
moreover, I thought there was a difference between the two below where one
would not let the value change whereas the other one would not let the
address of the pointer change, but I must be missing something here.

function3( const int* a)
a is a pointer to constant data (it would not let you change the pointed-to
value); everything is fine.
function4( int* const a )
a is a constant pointer to data; it would not let you change the /value/
of the pointer a (the address of a variable never changes). This const is
rather, sorry, pointless, as in the case of function1 and function2.

Denis
 
J

johny smith

Can someone please explain to me the difference between these two:

function1( const int a)
function2( int const a)

Both seemed to compile, but what is the difference between the two above.
And why would one choose one over the other.

moreover, I thought there was a difference between the two below where one
would not let the value change whereas the other one would not let the
address of the pointer change, but I must be missing something here.

function3( const int* a)


function4( int* const a )

Thanks in advance for any quidance here.
 
E

E. Robert Tisdale

johny said:
Can someone please explain to me the difference between these two:

int function1(const int a);
int function2(int const a);

Both seemed to compile
but what is the difference between the two above.
cat main.cc
int function(const int a);
int function(int const a);

int function(int a) {
return a;
}

int main(int argc, char* argv[]) {
return function(13);
}
g++ -Wall -ansi -pedantic -o main main.cc

There is no difference between any pair of declarations

int function(int a);
int function(const int a);

and

int function(int const a);
And why would one choose one over the other.
moreover, I thought there was a difference between the two below where one
would not let the value change whereas

the other one would not let the address of the pointer change,
but I must be missing something here.

int function3(const int* p);

This declaration is equivalent to

int function3(int const* p);

This means that function3(const int*) cannot change *p.
int function4(int* const p);

This *declaration* is equivalent to

int function4(int* p);

The difference is in the function *definition*

int function4(int* const p) {
*p = 13; // OK! *p is mutable
// . . .
++p; // error! p is const
return *p;
}
 
J

Jack Klein

Can someone please explain to me the difference between these two:

function1( const int a)
function2( int const a)

No difference at all here. According to the grammar, the 'const'
keyword modifies what is immediately to its left (before it). An
exception is made to allow it to be first.
Both seemed to compile, but what is the difference between the two above.
And why would one choose one over the other.

moreover, I thought there was a difference between the two below where one
would not let the value change whereas the other one would not let the
address of the pointer change, but I must be missing something here.

function3( const int* a)


function4( int* const a )

Thanks in advance for any quidance here.

What you thought was exactly correct:

const int *a; // a is a modifiable pointer to constant int
int const *a; // a is a modifiable pointer to constant int
int * const a; // a is a constant pointer to modifiable int
const int *const a; // a is a constant pointer to a constant int
int const *const a; // a is a constant pointer to a constant int

So what makes you think you are missing something?

>
 
J

John Harrison

johny smith said:
Can someone please explain to me the difference between these two:

function1( const int a)
function2( int const a)

Both seemed to compile, but what is the difference between the two above.
And why would one choose one over the other.

moreover, I thought there was a difference between the two below where one
would not let the value change whereas the other one would not let the
address of the pointer change, but I must be missing something here.

function3( const int* a)


function4( int* const a )

Thanks in advance for any quidance here.

What matters is whether const goes before or after *, whether it goes before
or after int is immaterial.

function3a( const int* a)

function3b( int const* a)

function4( int* const a)

3a and 3b are the same, 4 is different.

john
 
D

Dietmar Kuehl

johny smith said:
Can someone please explain to me the difference between these two:

function1( const int a)
function2( int const a)

First of all, 'const' applies to all variable declarations, not just to
the declaration of function parameters. In fact, the above use of 'const'
may be confusing because it does not change the signature of the function
but only applies to the use of the variables within the function. That is,
the 'const' only applies to the use of the parameter within the function,
and the function can be declared with or without the 'const' and then
defined with or without the 'const'.

Concerning your original question: There is no difference between these
two uses of 'const'. In both cases the object 'a' is made constant.
Both seemed to compile, but what is the difference between the two above.
And why would one choose one over the other.

My strong preference is to place the 'const' to the right because it is
consistent with other uses of 'const' where you don't have the choice:

- 'int* const', ie. a const pointer to a non-const 'int'; you cannot place
the 'const' differently to achieve the same effect.
- 'struct S { void f() const; };', ie. a const member function which actually
just means that the object pointed to by 'this' is 'const', ie. is
implicitly declared as 'S const* const this'; you cannot place the 'const'
differently.

In addition, it is pretty simple to decode what is made 'const': the type
immediately to the left of the 'const', e.g.:

- 'int const*' is a non-const pointer to a const 'int' (the pointer can be
changed but not the pointed to object)
- 'int* const' is a const poniter to a non-const 'int' (the pointer cannot
be changed but the pointer to object can)
- 'int const* const' is a const poniter to a const 'int' (neither the pointer
nor the object can be changed)

Another argument for my preference of the 'const' placement is soemthing
I stumbled over quite a while. Initially, I had code like this (the involved
types where something else than 'int' but this does not matter):

void f(const int*);

.... and a typedef was introduced:

typedef int* int_ptr;

.... and I modified the declaration to use this typedef:

void f(const int_ptr);

to use this typedef. Of course, I changed the semantics! The original
function took a pointer to a const 'int' as parameter. After the replacement
the function took a const pointer to a non-const 'int' as parameter. It
would have been obvious that this replacement won't work if the declaration
had been

void f(int const*);

in the first place.

The two arguments in favour of the other placement of 'const' are that C
uses this placement (but C has a much less elaborated notion of constness)
and that most people use it. To me, neither of these arguments are sufficient
to abandon the more readable and consistent notation.
moreover, I thought there was a difference between the two below where one
would not let the value change whereas the other one would not let the
address of the pointer change, but I must be missing something here.

function3( const int* a)
function4( int* const a )

The former lets the pointer change but not the pointee, the latter lets
the pointee change but not the pointer. It would have been more obvious
if the first of these declarations had used the more readable

function3( int const* a )

Also, the 'const' in the second declaration only applies to the definition
of the function.
 
J

JKop

E. Robert Tisdale posted:

There is no difference between any pair of declarations

int function(int a);
int function(const int a);

and

int function(int const a);


Yes there is.

int function(int a)
{
return ++a;
}


No problem.


int function(const int a)
{
return ++a;
}

Problem.


int function(int const a)
{
return ++a;
}

Problem.


-JKop
 
J

John Harrison

JKop said:
E. Robert Tisdale posted:




Yes there is.

int function(int a)
{
return ++a;
}


No problem.


int function(const int a)
{
return ++a;
}

Problem.


int function(int const a)
{
return ++a;
}

Problem.

Much as I hate to agree with Bob, he said 'no difference between the
declarations', you posted definitions. As prototypes these are all
identical.

int function(int a);
int function(const int a);
int function(int const a);

It's yet another obscure corner of C++, read first para of Dietmar's post.

john
 

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
474,174
Messages
2,570,941
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top