const int function parameter
Here's a question for anyone who knows about C/C++ technicalities. I recently noticed that gcc/g++ allows the following for ints or any other builtin type. type_name function( int ); type_name function( const int x ){ ... } i.e. the declaration and definition parameter types are nominally different. Is this correct in ISO C++? I couldn't find any mention of this in Stroustrup. It seems eminently sensible to me that this should be allowed. I often want the compiler to check that x is const within function but don't need to declare a variable const if it's passed by value. OTOH C++ allows overloading - type_name function( const int& x ){ ... } type_name function( int& x ){ ... } can be defined differently. -- JDL Non enim propter gloriam, diuicias aut honores pugnamus set propter libertatem solummodo quam Nemo bonus nisi simul cum vita amittit.
On Fri, 11 Jul 2003 17:48:29 +0100
John Lamb
Here's a question for anyone who knows about C/C++ technicalities.
I recently noticed that gcc/g++ allows the following for ints or any other builtin type.
type_name function( int );
type_name function( const int x){ ... }
i.e. the declaration and definition parameter types are nominally different. Is this correct in ISO C++? I couldn't find any mention of this in Stroustrup.
It seems eminently sensible to me that this should be allowed. I often
want the compiler to check that x is const within function but don't need to declare a variable const if it's passed by value. OTOH C++ allows overloading -
type_name function( const int& x ){ ... }
type_name function( int& x ){ ... }
can be defined differently. const int x is, as you mentioned, passed by value, but also does not allow the function to set x to another value (in C and C++). This is quite different from the C++ call by reference. In the pass by reference model, making chages to x side effects the caller's x. So, "const int& x" is much more important than is const int x. Also, in both C and C++, making a pointer constant: type_name function( const int *x ){ ... } This makes the int pointed to by x const, but the pointer, x, can be set and reused within the function.
Most of the C library function use const when that function is not going
to side effect:
eg. char *strcpy(char *d, const char *s);
This means that strcpy will not side effect the (source) string pointed
to by s.
--
Jerry Feldman
Jerry Feldman wrote:
Also, in both C and C++, making a pointer constant: type_name function( const int *x ){ ... } This makes the int pointed to by x const, but the pointer, x, can be set and reused within the function.
I wasn't thinking about pointers, but I guess it also makes sense to declare type_name function( const int *x ); and define type_name function( const int * const x ){ ... } if you know you're not going to reuse the pointer. I guess the advantage is that it's one less thing to debug or an easy way to check the pointer isn't being reused in a long function. -- JDL Non enim propter gloriam, diuicias aut honores pugnamus set propter libertatem solummodo quam Nemo bonus nisi simul cum vita amittit.
I wasn't thinking about pointers, but I guess it also makes sense to declare type_name function( const int *x ); and define type_name function( const int * const x ){ ... }
if you know you're not going to reuse the pointer. I guess the advantage is that it's one less thing to debug or an easy way to check the pointer isn't being reused in a long function. That is correct. The point that I wanted to make is that "const int *x" in a function prototype (or "const int& x") communicates to the caller
On Sat, 12 Jul 2003 00:10:08 +0100
John Lamb
Jerry Feldman wrote:
That is correct. The point that I wanted to make is that "const int *x" in a function prototype (or "const int& x") communicates to the caller that the object that x points to will not be modified by the function. It also allows the caller to use a pointer to a const object. I got the point and it's a very good answer to a very important question. But I really wanted to know about a trivial technicality: gcc doesn't distinguish, for example, between the following declarations.
int g( int (*const)(int x) ); int g( int (*f)(int const) ); Is this correct behaviour? I'm convinced it doesn't cause any problems (for example conflicting with overloading) bcause both declarations would resolve to int g( int (*)(int) ); it's also plausibly useful, though in the following example, gcc would barf on g but not on f. int f( const int x ); int f( int x ){ return ++x; } int g( int x ); int g( const int x ){ return ++x; } int g( int (*const)( int x ) ); -- JDL Non enim propter gloriam, diuicias aut honores pugnamus set propter libertatem solummodo quam Nemo bonus nisi simul cum vita amittit.
question. But I really wanted to know about a trivial technicality: gcc doesn't distinguish, for example, between the following declarations.
int g( int (*const)(int x) ); int g( int (*f)(int const) );
Is this correct behaviour? In both cases, you are prototyping a function that takes a function
On Sat, 12 Jul 2003 15:09:47 +0100
John Lamb
Jerry Feldman wrote:
int g( int (*const)(int x) ); int g( int (*f)(int const) );
Is this correct behaviour?
In both cases, you are prototyping a function that takes a function pointer as an argument. In the first case, I believe that you are making the function pointer const, and in the second case, you are making the argument to the function pointed to const.
Not having tested it with GCC, but I really think this is a trivial issue (at least with the int argument). I also think that there is room for the compiler to misinterpret.
However, if you want to discuss this further we can do it in person. The BLU is having a BarBQ today (http://www.blu.org). Why not grab the closest Jet and fly up to Boston :-). The weather is great today.
I've managed to check this againt the 1996 draft standard: a const qualifier doesn't affect the function type, only the behaviour of the parameter within the body of the function. So gcc is behaving correctly. You are of course correct on all three points: this is a trivial issue, there is room for a compiler to misinterpret and I really should get out more often ;-). Sadly Boston is just a little too far for a day trip. -- JDL Non enim propter gloriam, diuicias aut honores pugnamus set propter libertatem solummodo quam Nemo bonus nisi simul cum vita amittit.
participants (2)
-
Jerry Feldman
-
John Lamb