www.digitalmars.com         C & C++   DMDScript  

c++ - const in parameter by value

reply Heinz Saathoff <hsaat bre.ipnet.de> writes:
Hi Walter,

I ran into a problem compiling foreign C-code. The authors write code 
like this:

  ------ Header file  pro.h ----
  typedef struct {
     int a, b, c;
  } ABC;
  void Func(ABC * ap, int ini);
  
  ----- Implementation  pro.c -----
  #include "pro.h"
  
  void Func(ABC * const ap, int ini)
  /*              ^^^^^^  */
  {
     ap->a = ini;
     ap->b = ini;
     ap->c = ini;
  }/*Func*/

According to postings in comp.lang.c this seems to be legal in 
ANSI-C.  DMC complains about const vs. non-const. It makes sense to 
treat both function prototypes the same as the passed by value 
parameters can't change from the callers view. Using const in the 
definition is a promise to the compiler that the passed value should not 
be modified in the function body. May also be a hint to the optimizer to 
generate better code?

I think this is also true for C++ code.


	Heinz



I would expect that this  
Mar 18 2002
next sibling parent Heinz Saathoff <hsaat bre.ipnet.de> writes:
Heinz Saathoff schrieb...
 
 I think this is also true for C++ code.

I've just looked to the C++ standard. It's true here also. In chapter 8.3.5 (3) Functions I found this: "After producing the list of parameter types, several transformations take place upon these types to determine the function type. Any cv-qualifier modifying a parameter type is deleted. [Example: the type void (*)(const int) becomes void (*)(int) -end example]. Such cv-qualifiers affect only the definition of the parameter within the body of the function; they do not affect the function type." So this declarations are equal: void func(int); void func(const int); void func(volatile int); Regards, Heinz
Mar 18 2002
prev sibling parent "Walter" <walter digitalmars.com> writes:
I've never run into that before. Thanks for posting it. I'll add it to the
list to be fixed. -Walter

"Heinz Saathoff" <hsaat bre.ipnet.de> wrote in message
news:MPG.16ffcfd81cfe4dc198969e news.digitalmars.com...
 Hi Walter,

 I ran into a problem compiling foreign C-code. The authors write code
 like this:

   ------ Header file  pro.h ----
   typedef struct {
      int a, b, c;
   } ABC;
   void Func(ABC * ap, int ini);

   ----- Implementation  pro.c -----
   #include "pro.h"

   void Func(ABC * const ap, int ini)
   /*              ^^^^^^  */
   {
      ap->a = ini;
      ap->b = ini;
      ap->c = ini;
   }/*Func*/

 According to postings in comp.lang.c this seems to be legal in
 ANSI-C.  DMC complains about const vs. non-const. It makes sense to
 treat both function prototypes the same as the passed by value
 parameters can't change from the callers view. Using const in the
 definition is a promise to the compiler that the passed value should not
 be modified in the function body. May also be a hint to the optimizer to
 generate better code?

 I think this is also true for C++ code.


 Heinz



 I would expect that this

Mar 18 2002