Topic: Function declarations: const formals and non-const actuals
Author: "V.A.Neelesh" <v_a_neelesh@hotmail.com>
Date: 1997/11/15 Raw View
This is not a good practice and compiler should definetly gerenate a
warning.
By declaring 'f (int parameter)' , not only the parameter is passed by
"value" but also it can be changed inside the function.
By definig 'f(const int parameter), though the parameter is passed by
'value', it cannot be changed inside the function.
Thus the declaration and definition are meaning two differnet thing.
Neelesh
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/11/17 Raw View
[Followup redirected to comp.lang.c.moderated, as this is
purely a C question]
V.A.Neelesh <v_a_neelesh@hotmail.com> writes:
> This is not a good practice and compiler should definetly gerenate a
> warning.
It's exactly the opposite: doing the contrary (const int
parameter in function declaration) is bad practice and
could gerenate a warning (well, if the code is outside a
template).
> By declaring 'f (int parameter)' , not only the parameter is passed by
> "value" but also it can be changed inside the function.
How the function do its job is an implementation detail, and
should not appear in the interface. This is the base of procedural
decomposition in C.
What type of arguments the function takes is part of its interface
(example: can you call it with a const int* and a int* or only
a int*).
> By definig 'f(const int parameter), though the parameter is passed by
> 'value', it cannot be changed inside the function.
>
> Thus the declaration and definition are meaning two differnet thing.
Yes. The definition means something, while the declaration
means nothing, and something and nothing are two differnet
things.
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Per Erik Stendahl <berrs@cyberdude.comyouknowwhat>
Date: 1997/11/17 Raw View
V.A.Neelesh wrote:
>
> This is not a good practice and compiler should definetly gerenate a
> warning.
> By declaring 'f (int parameter)' , not only the parameter is passed by
> "value" but also it can be changed inside the function.
>
> By definig 'f(const int parameter), though the parameter is passed by
> 'value', it cannot be changed inside the function.
>
> Thus the declaration and definition are meaning two differnet thing.
I do not agree. When 'parameter' is passed-by-value it is copied into
a local variable i 'f'. Whatever happens to this local variable, it
cannot
affect the calling function. To anyone but f itself there is no
difference
between f(int) and f(const int).
Remember, this is pass-by-value, not pass-by-reference.
Regards,
Per Erik Stendahl
Swedish Insitute of Computer Science
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Per Erik Stendahl <berrs@cyberdude.comyouknowwhat>
Date: 1997/11/05 Raw View
Hi.
Is this legal C++?
// Declare f
void f(int parameter);
// Define f
void f(const int parameter)
{
// ...
}
Since the int is passed by-value, it does not make a difference
for the calling function (which only sees the declaration) whether
the formal parameter is const in the function definition, does it?
g++ swallows this without problems, Sun's compiler (what is it called
anyway? Sunpro? Sparcworks?) gives me warnings and HP's C++ compiler
aborts saying that the overloading mechanism cannot distinguish between
'int' and 'const int'.
Regards,
Per Erik Stendahl,
Swedish Institute of Computer Science
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1997/11/07 Raw View
Valentin Bonnard <bonnardv@pratique.fr> writes:
|> Per Erik Stendahl wrote:
|>
|> > Is this legal C++?
|> >
|> > // Declare f
|> > void f(int parameter);
|> >
|> > // Define f
|> > void f(const int parameter)
|> > {
|> > // ...
|> > }
|> >
|> > Since the int is passed by-value, it does not make a difference
|> > for the calling function (which only sees the declaration) whether
|> > the formal parameter is const in the function definition, does it?
|>
|> This is, and has always been correct C++. A number of compilers are
|> known to be buggy in this area, perhpas because the std first says
|> 'shall match exactly' then says that top-level const doesn't count.
This is certainly correct C++. As for "has always been", I don't think
that the ARM addressed the question. And the fact that the HP compiler
forbids it, and Sun warns, makes me think that CFront probably didn't
accept it. (5 or more years ago, CFront was more or less the defacto
reference compiler.)
--
James Kanze +33 (0)1 39 23 84 71 mailto: kanze@gabi-soft.fr
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
I'm looking for a job -- Je recherche du travail
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/11/05 Raw View
Per Erik Stendahl wrote:
> Is this legal C++?
>
> // Declare f
> void f(int parameter);
>
> // Define f
> void f(const int parameter)
> {
> // ...
> }
>
> Since the int is passed by-value, it does not make a difference
> for the calling function (which only sees the declaration) whether
> the formal parameter is const in the function definition, does it?
This is, and has always been correct C++. A number of compilers are
known to be buggy in this area, perhpas because the std first says
'shall match exactly' then says that top-level const doesn't count.
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]