why arguments are not incompatible

S

sumsin

Why the below two code snippet behave differently

Case 1:
#include <iostream>

void foo(const int *i)
{
int *local = i;
}

int main(void)
{
return 0;
}


and Case 2:
#include <iostream>

void foo(const int i)
{
int local = i;
}


int main(void)
{
return 0;
}

compilation command
g++ -Wall test03.cpp -o test03

In case 1 I got the below error.
error: invalid conversion from `const int*' to `int*'

In my understanding for an assignment:
- first the lvalue are rvalue must be of compatible type without
considering the qualifiers.
- then the lvalue must have all or more qualifiers than rvalue.

So its ok that I getting an error for case 1 but why its not happening
in case 2.

please clarify...
 
E

Eric Pruneau

sumsin said:
Why the below two code snippet behave differently

Case 1:
#include <iostream>

void foo(const int *i)
{
int *local = i;
}

int main(void)
{
return 0;
}


and Case 2:
#include <iostream>

void foo(const int i)
{
int local = i;
}


int main(void)
{
return 0;
}

compilation command
g++ -Wall test03.cpp -o test03

In case 1 I got the below error.
error: invalid conversion from `const int*' to `int*'

In my understanding for an assignment:
- first the lvalue are rvalue must be of compatible type without
considering the qualifiers.
- then the lvalue must have all or more qualifiers than rvalue.

So its ok that I getting an error for case 1 but why its not happening
in case 2.

please clarify...

in case 2, local is a copy of i, so local has nothing to do with i except
that it is initialized with the same value.
So changing the value of local doesn't affect i.

But if you do

void foo(const int i)
{
int& local = i;
}

it wont work cause now local is a reference to i and therefore changing
local cause i to change. It was not the case with your original example.
 
T

Tomás Ó hÉilidhe

void foo(const int *i)
{
  int *local = i;

}

The following is a non-const pointer to a non-const int:

int *p;

The following is a const pointer to a non-const int:

int *const p;

The following are a non-const pointer to a const int:

int const *p;
const int *p;

The following are const pointers to const int's:

int const *const p;
const int *const p;

The problem with your code is that you try to use a "pointer to non-
const" to store the address of a const int. C doesn't allow this,
because it could potentially allow you to alter const data.
 
M

metarox

The compiler sees this for #2

void foo(int i)
{
int local = i;

}

Using const with a pass by copy argument is useless.
 
T

Tomás Ó hÉilidhe

Why is it useless?
It seems like there are lots of cases where you may have a variable passed
into a function where the function does not need to change it that a const
value may make the code more readable and less error prone in matainence.

For some strange reason there seems to be a sizeable amount of people
who are vehemently against using const in a function's parameter list.
I don't know why, I mean I treat function arguments just like normal
automatic variables and so I'll use const where appropriate.
 
J

James Kanze

For some strange reason there seems to be a sizeable amount of
people who are vehemently against using const in a function's
parameter list. I don't know why, I mean I treat function
arguments just like normal automatic variables and so I'll use
const where appropriate.

A surprising number of people don't use const on normal
automatic variables either:).

In the case of parameters, there is a strong argument against
using const in the declarations (what the client code sees),
where it has no effect other than adding verbiage and creating
(a very little) confusion, and probably exposing some interior
details of the function which aren't relevant. And a number of
people insist that the signatures in the definition correspond
letter for letter to those in the declaration.

And of course, the usefulness of const is related to contract,
more than anything, and you don't establish a contract for local
variables. (That doesn't mean that they should never be const.
Only that const-ness typically isn't anywhere near as important
with them.)
 
F

Frank Birbacher

Hi!
True, the function signatures, as pure declarations,

void foo( int );

and

void foo( int const );

are equivalent.

That does not mean that the 'const' has no effect in a function definition.

Which reads:

void foo1( int i )
{
if(i = 5) //allowed
{
bar();
}
}

void foo2( const int i )
{
if(i = 5) //error
{
bar();
}
}
Sorry, that's incorrect.

Yes, it has its usage.

Frank
 

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
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top