20

I have the following function declaration in a header file:

extern void flash(const char *message, const enum msg_type type);

Basically, it takes two parameters and pushes a corresponding message into a global message queue. Since it doesn't need to modify the parameters, I const-qualified them. However, CLion's static code analyzer emitted a warning about it:

Clang-Tidy: Parameter 'type' is const-qualified in the function declaration; const-qualification of parameters only has an effect on function definitions

enter image description here

Here is my questions:

  1. I const-qualified both parameters, why does only the latter trigger a warning?
  2. Is it really bad? I know it has no effect, but technically specking const qualifiers have no effect too.
  3. Can I get rid of this warning?
2
  • 4
    You did not const-qualify the first parameter. You added const behind a pointer type
    – M.M
    Commented Jul 16, 2018 at 3:29
  • @M.M You are right. Actually I’m qualifying what the first parameter points to.
    – nalzok
    Commented Jul 16, 2018 at 3:32

2 Answers 2

23

The first parameter is of type const char *, or pointer to constant character. This means that you can pass into the function a pointer to a string which you can't modify, for example:

const char* msg = "Hello, world!";
flash(msg, SOME_MESSAGE_TYPE);

You can't change the characters in msg; it's a pointer to const char. Passing it to a function with parameter type char* would indicate the function might change them, which would be illegal. This const in the parameter type is relevant to the caller, so is kept.

On the other hand, enum msg_type is just an enum, and will be copied into the function. When calling the function, I don't care what happens in the body of the function with type; it won't affect anything outside the function. Saying that this is const doesn't make a difference, hence the warning.

If you change the first parameter to const char *const message, then it'll warn about that too. That would indicate that you can't change what the pointer message points to, which again the caller doesn't care about because whatever pointer it passes in won't change.


This isn't really bad; it's telling you that you might be confused, but in this case it doesn't hurt anything. You should still get rid of the warning, though, because warnings indicate potential problems and clogging them with non-problematic noise just makes it less likely you'll read the important ones.


Change the header file, but not wherever flash is implemented, to not have const on the second parameter. Where it's implemented, keep the const so you don't actually change type inside the function body, but it's not needed in the declaration.

12
  • 1
    +1 didn't know this subtle difference between function definition (which specifies the internal implementation) and declaration (which specifies the external interface)
    – nalzok
    Commented Jul 16, 2018 at 2:44
  • 1
    @chux I knew I should have double-checked. I primarily use C++ and knew there were differences in the details, but I thought I was sticking to something safe (at least by C11) when I said that. Since this is about the type system and not UB, I reworked the example to not use a string literal directly.
    – Daniel H
    Commented Jul 16, 2018 at 4:09
  • 1
    const values in declarations do not affect the signature of a function, so you should not be put in the function declaration. For best practice, the value should pass by reference.
    – Arun Kumar
    Commented Jun 27, 2019 at 12:53
  • @Arun The question is tagged C, not C++; C doesn't have references. Even if it did, msg is already a pointer and it's probably better performance to pass an enum by value.
    – Daniel H
    Commented Jun 28, 2019 at 18:28
  • Seems odd to me that we are supposed to make the declaration not match the implementation, but there's a lot of weird stuff in C/C++ so I guess I just call it "idiomatic style" and roll with it. Commented Dec 22, 2020 at 3:38
3

I const-qualified both parameter, why does only the latter trigger a warning?

As your warning says, it does not affect a prototype. It only affects the implementation.

Is it really bad?

It is noise in the meaning that it does not affect anything, but other than that, no.

Can I get rid of this warning?

You can safely remove the const qualifier since it is not needed.

However, it seems a bit messy to suppress warnings in general with clang-tidy. This link might help:

clang-tidy: How to suppress warnings?

But these warnings can actually be a blessing. It's not very uncommon to accidentally write int foo(const char *) instead of int foo(char * const). The latter would not trigger this warning, so if you get this warning it's a sign that you have mixed something up.

2
  • What is wrong with saying const char * if my intention is that the characters will not be modified? Commented Jun 22, 2023 at 19:22
  • @user129393192 There is nothing wrong with that
    – klutt
    Commented Jun 23, 2023 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.