Archives
D Programming
DD.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
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 schrieb...I think this is also true for C++ code. Mar 18 2002
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
|