www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - const in parameter by value

↑ ↓ ← 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
→ 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
→ "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