0

Please somebody explain me why do we have two const declarations in the first parameter of the function

what difference would it make if we write the first parameter like this "const uint8_t*"

bool Buffer::push(const uint8_t* const input, const size_t input_size)
shomit
  • 209
  • 1
  • 7
  • 2
    It basically means you are neither allowed to change `input`, nor the data it's pointing to. But passing by const value isn't really useful, afaik. – Lukas-T Dec 21 '20 at 07:16
  • @churill Passing by const value is useful sometimes: It ensures that the implementation will not change the value of the parameter. Of course, it does not make a difference to the caller, because the innermost const in the given examples is not part of the function signature. – j6t Dec 21 '20 at 07:32
  • @j6t True, but if a function takes a parameter by value, i.e. makes a copy of it, I don't see any reason, why it shouldn't be able to modify this local copy. Maybe due to historical reasons, to help compilers optimize things. – Lukas-T Dec 21 '20 at 08:29
  • @churill It can be used for the same reason that people write `const auto N = foo.size();`: they want to document that the value is not going to be changed (and the compiler ought to bark if an attempt to change it is made). And, yes, it opens optimization possibilities, because const objects must not be changed, not even after casting away const-ness. – j6t Dec 21 '20 at 15:28

0 Answers0