By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,997 Members | 1,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,997 IT Pros & Developers. It's quick & easy.

order of const in function prototypes

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
johny smith wrote:
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;
}
Jul 22 '05 #2

P: n/a
johny smith wrote:

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
Jul 22 '05 #3

P: n/a
On Tue, 29 Jun 2004 20:15:51 -0600, "johny smith"
<pr**************@charter.net> wrote in comp.lang.c++:
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?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html>

Jul 22 '05 #4

P: n/a

"johny smith" <pr**************@charter.net> wrote in message
news:10*************@corp.supernews.com...
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
Jul 22 '05 #5

P: n/a
"johny smith" <pr**************@charter.net> wrote:
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.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
Jul 22 '05 #6

P: n/a
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
Jul 22 '05 #7

P: n/a

"JKop" <NU**@NULL.NULL> wrote in message
news:lH*****************@news.indigo.ie...
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.


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
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.