Topic: implicit conversion to const?


Author: stan@chirpa.Mayo.EDU (Andrew Staniszewski)
Date: 1996/05/24
Raw View
Hi,
I have run accross some irritating behavior related to const parameters
on a function call. My example is:

-----------------------------------------------------------------------------
int foo(const char *const *value);

int bar()
{
  char *argv[7];
  foo(argv);
  return 0;
}
-----------------------------------------------------------------------------



I have access to several platforms with differnt compilers, and have
found that the older compilers will compile the code with out
complaint, but more recent compilers complain about the call to foo
because of the type mismatch (const char * const *  vs char **)

I have been unable to find in the ARM mention that a standard
conversion from value to const value exists, but I certainly think it
out exist, otherwise useing const in function declartions become yucky,
and (in my mind) unecessary casts to const types would be required when
making the function calls.




------------------------------------------------------------------------------
Andrew Staniszewski
Sr. Software Engineer
Special Purpose Processing Group
Mayo Foundation
staniszewski.andrew@mayo.edu

Please Note: Unless otherwise specified, I speak only for myself, NOT as
a representative of the Special Purpose Processor Group or Mayo Foundation

------------------------------------------------------------------------------
------------------------------------------------------------------------------
           A diamond is a lump of coal that stuck with the job
------------------------------------------------------------------------------
------------------------------------------------------------------------------
---
[ 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: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/05/25
Raw View
stan@chirpa.Mayo.EDU (Andrew Staniszewski) writes:

>I have run accross some irritating behavior related to const parameters
>on a function call. My example is:
>
>int foo(const char *const *value);
>
>int bar()
>{
>  char *argv[7];
>  foo(argv);
>  return 0;
>}

This example is well-formed (i.e. legal) according to the draft C++ standard.

>I have access to several platforms with differnt compilers, and have
>found that the older compilers will compile the code with out
>complaint, but more recent compilers complain about the call to foo
>because of the type mismatch (const char * const *  vs char **)

I suspect that you may be mistaken --- I would expect the situation to
be the reverse of what you describe.  Your example code is not legal in
C and was not legal in C++ until recently, so I would expect older
compilers to reject it, but its legal in C++ now, so I would expect
newer compilers to accept it.

>I have been unable to find in the ARM mention that a standard
>conversion from value to const value exists, but I certainly think it
>out exist, otherwise useing const in function declartions become yucky,
>and (in my mind) unecessary casts to const types would be required when
>making the function calls.

The relevant ARM section is 4.6: Pointer Conversions.
Your example is not allowed by the ARM, but it is allowed by
the corresponding section of the draft standard, section
4.4 [conv.qual]: Qualification Conversions.

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ 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                             ]