[hipl-dev] Re: [Merge] lp:~hipl-core/hipl/libhip into lp:hipl

  • From: Xin Gu <eric.nevup@xxxxxxxxx>
  • To: hipl-dev@xxxxxxxxxxxxx
  • Date: Mon, 27 Feb 2012 11:27:11 +0200

Hi,

On 26/02/12 22:34, Stefan Götz wrote:
I was wondering if we can just use "const void *ptr"? I haven't figured
out why we need the second const.
http://www.parashift.com/c++-faq-lite/const-correctness.html

In short, 'const int *ptr' makes '*ptr = 7' illegal. 'int *const ptr' makes 'ptr
= NULL' illegal.

If I understand correctly, you also suggest to change "struct
hip_packet_context *ctx" to "struct hip_packet_context *const ctx" for
the following function. Then I have the same question related to const
after asterisk.

int hip_read_control_msg_tcp(int sockfd, struct hip_packet_context *ctx)
Actually: const int sockfd, struct hip_packet_context *const ctx

Makes assignments to sockfd and ctx illegal because such assignments are not
necessary, anyway.



I understand the syntax of const, but from the caller's point of view, it has no difference if the function parameters are "const int sockfd, struct hip_packet_context *const ctx" or "int sockfd, struct hip_packet_context *ctx". Because both integer and pointer are pass-by-value, and callee changing those parameters or not won't affect caller.

That's why I am wondering if it is really necessary to add const under those situations, if we have a look at some other C libraries, it is also rare to see them using const in this way. For example, socket(int, int, int) not socket(const int, const int, const int).

Of course, a const pointer like "const char *ptr" is necessary, which means the function will not alter the data this ptr points to. My commit last night fixed the const missing for this case.


Xin

Other related posts: