Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > f(int i) declaration, f(const int i) definition?

Reply
Thread Tools

f(int i) declaration, f(const int i) definition?

 
 
dean
Guest
Posts: n/a
 
      03-27-2008
We were working through one of Sutter's books this lunch time, and it
came about that you can put a function:

void f(int i); // in the header file

and

void f(const int i) {} // as the definition.

My question is: is that correct? Isn't the type const int different
from just int? I presume that inside function f, one could not change
i if it's marked as const in the declaration, even if its marked so
ONLY in the declaration.

-Thanks for reading!
-Dean
 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      03-27-2008
On Wed, 26 Mar 2008 18:58:58 -0700 (PDT), dean <>
wrote in comp.lang.c++:

> We were working through one of Sutter's books this lunch time, and it
> came about that you can put a function:
>
> void f(int i); // in the header file
>
> and
>
> void f(const int i) {} // as the definition.
>
> My question is: is that correct? Isn't the type const int different
> from just int? I presume that inside function f, one could not change
> i if it's marked as const in the declaration, even if its marked so
> ONLY in the declaration.


A top-level const qualifier has no meaning on a function parameter. An
int, or any other object type, is passed by value. The int object
that f() receives is its very own copy, so whatever f() does to its
'i' has no effect on the caller.

The const qualifier in the definition will prevent f() from changing
its 'i'.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
Reply With Quote
 
 
 
 
Chris Thomasson
Guest
Posts: n/a
 
      03-27-2008

"Jack Klein" <> wrote in message
news...
> On Wed, 26 Mar 2008 18:58:58 -0700 (PDT), dean <>
> wrote in comp.lang.c++:
>
>> We were working through one of Sutter's books this lunch time, and it
>> came about that you can put a function:
>>
>> void f(int i); // in the header file
>>
>> and
>>
>> void f(const int i) {} // as the definition.
>>
>> My question is: is that correct? Isn't the type const int different
>> from just int? I presume that inside function f, one could not change
>> i if it's marked as const in the declaration, even if its marked so
>> ONLY in the declaration.

>
> A top-level const qualifier has no meaning on a function parameter. An
> int, or any other object type, is passed by value. The int object
> that f() receives is its very own copy, so whatever f() does to its
> 'i' has no effect on the caller.
>
> The const qualifier in the definition will prevent f() from changing
> its 'i'.

[...]

FWIW, I find that useful in C; basically, to mimic the 'this' pointer in
C++. Something like:
__________________________________________________ ___________
typedef struct foo_s foo;

struct foo_s {
[...];
};

void
foo_do_something(
foo* const _this,
) {
[...]
}
__________________________________________________ ___________



I am not allowed to mess with the '_this' parameter in the definition of the
'foo_do_something()' function.

 
Reply With Quote
 
Chris Thomasson
Guest
Posts: n/a
 
      03-27-2008

"Chris Thomasson" <> wrote in message
news:. ..

> __________________________________________________ ___________
> typedef struct foo_s foo;
>
> struct foo_s {
> [...];
> };
>
> void
> foo_do_something(
> foo* const _this,

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

of course that comma is not supposed to be there!

ARGH!


> ) {
> [...]
> }
> __________________________________________________ ___________
>

 
Reply With Quote
 
dean
Guest
Posts: n/a
 
      03-27-2008
> A top-level const qualifier has no meaning on a function parameter. An
> int, or any other object type, is passed by value. *The int object
> that f() receives is its very own copy, so whatever f() does to its
> 'i' has no effect on the caller.
>
> The const qualifier in the definition will prevent f() from changing
> its 'i'.
>


Yes, I can see that, I was just questioning why the definition is
allowed to change from the declaration. The writer of the function may
want to indicate that the parameter won't be changed throughout the
function body, for example if its an index into an array.
 
Reply With Quote
 
ppi
Guest
Posts: n/a
 
      03-27-2008
> Yes, I can see that, I was just questioning why the definition is
> allowed to change from the declaration. The writer of the function may
> want to indicate that the parameter won't be changed throughout the
> function body, for example if its an index into an array.


As J.Klein said, top-level const qualifier has no meaning on the
function parameter:
you can rephrase that by saying that f( int a ) and f( const int a )
have the same signature from the compiler point of view.
so When you declare:
int loop( int a );
int loop( const int a );

for the compiler this is the same declaration. But if you try:
int loop( int a )
{
return a+1;
}

int loop( const int a )
{
return a+2;
}

Boom ! This is flagged as defining twice the same function.

From my gcc compiler:

int loop( int a );
int loop( const int a );

int loop( int a )
{
return a+1;
}

int loop( const int a )
{
return a+2;
}


c++ -c loop.cpp
loop.cpp: In function 'int loop(int)':
loop.cpp:9: error: redefinition of 'int loop(int)'
loop.cpp:4: error: 'int loop(int)' previously defined here

c++ --version gives me:
c++ (GCC) 4.2.3

So yes for functions passing parameters by value you may end up by
defining a function with different function bodies (const / non const
index inside the function).
Note that as previously said, the caller does not care what you do
inside the function since its parameter is not changed.
But you, the implementor, you have more freedom in defining the
content of the function.

-- paulo
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      03-29-2008
dean wrote:
> We were working through one of Sutter's books this lunch time, and it
> came about that you can put a function:
>
> void f(int i); // in the header file
>
> and
>
> void f(const int i) {} // as the definition.
>
> My question is: is that correct? Isn't the type const int different
> from just int?


Well, there's no definitive answer to that question within the C++98
language specification. It is not clear on this, which is recognized in
defect report #140

http://www.open-std.org/jtc1/sc22/wg...fects.html#140

According to the original intent, function declarations should be
matched to each other in accordance with their adjusted parameter lists,
i.e. after (not before) all array and function types are replaced with
corresponding pointer types and all top-level cv-qualifiers are removed.
The original version of the standard failed to address this
"before/after" ambiguity.

I don't know when the changes suggested by DR#140 were incorporated into
the document, but I can see them in N2461.

The compilers followed the intended approach from the very beginning, so
you can use it safely.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      03-30-2008
Jack Klein wrote:
> ...
> A top-level const qualifier has no meaning on a function parameter. An
> int, or any other object type, is passed by value. The int object
> that f() receives is its very own copy, so whatever f() does to its
> 'i' has no effect on the caller.
> ...


The fact that the local parameter value doesn't change can still be quite useful
from the caller's point of view. While it is true that at the C++ language level
this to have ho effect on the caller, it still can be used to generate better
caller code. In traditional implementation the caller prepares the arguments in
a special "argument area" (stack frame, for example), which is later directly
accessed by the function as its local parameters. If some parameter is declared
as 'const', the caller can be sure that it is not changed by the function and
therefore can be reused for the several consecutive function calls.

For example,

void foo(const int i, int j) {
...
}

...

int a = 5;
for (int b = 0; b < 1000; ++b)
foo(a, b);

In this the compiler can prepare the 'i' parameter for the future 'foo' calls
only once, before the cycle begins, instead of re-initializing it on every
iteration.

Unfortunately, the declaration matching rules in C/C++ are to loose to use this
kind of optimization reliably: the compiler has to see the _definition_ of the
function in order to be sure that the parameter is indeed declared as 'const'.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why is int a[0] not allowed, but int* a = new int[0] is? haijin.biz@gmail.com C++ 9 04-17-2007 09:01 AM
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
int a[10]; int* p=(int*)((&a)+1); But why p isn't equal to ((&a)+1)? aling C++ 8 10-20-2005 02:42 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57