0

I have a habit to declare function parameters as const references (for const functions mostly, or if a parameter is purposefully read-only). Firstly because I think it ensures that the object is immutable. Secondly because it makes me think that this prevents copying the variable, especially when it's already read-only (even though the function works with the object of primitive data type).

But what if I pass a non-const variable to the function, or a pointer? Will it cause type casting, and are there consequences?

xt1zer
  • 35
  • 2
  • 7
  • 2
    unless you need to modify the passed in object, passing by const reference is the go-to solution – NathanOliver May 04 '22 at 16:02
  • 1
    Adding const to a reference only tells the compiler to disallow modifications through that reference, it doesn't actually do anything on the CPU. There is no cost associated with it. – Jonathan S. May 04 '22 at 16:03
  • 1
    a const reference cannot make the underlying object immutable. It is only a promise that the object will not be mutated *via that reference*. However, no casting or other legwork is necessary, exactly because of the weakness of that guarantee. – Karl Knechtel May 04 '22 at 16:03
  • 5
    In fact, (im)mutability isn't a real property of data in the first place. It's an abstraction that exists *entirely* within the compiler's paradigm. Objects are only "immutable" insofar as the compiler refuses to emit instructions that would modify the underlying memory. – Karl Knechtel May 04 '22 at 16:05
  • There won't be any overhead because of casting, the compiler can handle that seamlessly with lvalue parameters. Seemingly, we don't have an appropriate duplicate for this question. – πάντα ῥεῖ May 04 '22 at 16:07
  • Applying the closest, with best explanations for differences regarding pass by value for primitive types. – πάντα ῥεῖ May 04 '22 at 16:18
  • There are use cases for all the different ways to pass in a parameter. In my experience, the most common case is `const&` as your *go to* choice, followed closely with passing primitives types *by value*. Then, if the circumstances warrant otherwise (which is plenty common too), to consider the other ways to pass in a parameter as appropriate for what is needed. – Eljay May 04 '22 at 16:44
  • Whether a const object is immutable or not depends on your definition of immutable: Any `mutable` member variables can still be modified. Furthermore doesn't prevent you from copying the variable. in fact `auto x = arg0;` will always result in a copy or a compiler error, if `arg0` is a const ref. Furthermore it doesn't necessarily prevent the creation of a new object on a function call: `void f(std::string const&){} ... f("Hello World");` results in the creation of a new `std::string` object which contains a copy of the `"Hello World"` data. – fabian May 04 '22 at 16:46
  • It will not cause type casting. A cast is something you write in your code to tell the compiler to do a conversion. The question is about **conversions**. – Pete Becker May 04 '22 at 16:47

0 Answers0