Use of 'Const' For Function Parameters

Use of 'const' for function parameters

The reason is that const for the parameter only applies locally within the function, since it is working on a copy of the data. This means the function signature is really the same anyways. It's probably bad style to do this a lot though.

I personally tend to not use const except for reference and pointer parameters. For copied objects it doesn't really matter, although it can be safer as it signals intent within the function. It's really a judgement call. I do tend to use const_iterator though when looping on something and I don't intend on modifying it, so I guess to each his own, as long as const correctness for reference types is rigorously maintained.

Why using const for arguments passed by value? [duplicate]

I can think of a few reasons:

1) When someone reads the code and see const T a, they know that a should not be modified in the body of the function.

2) The compiler will tell you when you try to modify a in the body of the function. Therefore, adding const can prevent mistakes.

BTW chris already mentioned this in the comments.

3) However, there is another difference in C++11. A constant object cannot be moved from, as a move operation modifies the object. Therefore, you can only make a copy of a in the function body and cannot move from it.

4) Also, if this is a class type, you cannot call non-const members functions on a const object.

Why should I not use `const` in this simple function?

From the caller's perspective, both the first and the second form are the same.

Since the integers are passed by value, even if the function modifies a and b, the modified values are copies of the original and won't be visible to the caller.

However, from the function implementer's perspective there's a difference. In fact, in the second form:

int add(const int a, const int b)

you will get a compiler error if you try to modify the values of a and b inside the function's body, as they are marked as const.

Instead, you can change those values if you omit the const.

Again, those modifications will not be visible to the caller.

C++ Const Correctness in function parameters or in declartions

const at the end applies to this (making it pointer-to-const), meaning the member function will not modify the object on which it is called

class Cls {
public:
void f() {
++x_; // no problem
};

void g() const {
++x_; // error, can't modify in const member function
};

private:
int x_{};
};

In your example, you want to say both that the parameter is const, as well as this. In lhs == rhs, lhs is treated as const only if you have the trailing const, so you are right to use

bool operator==(const rational<T>& rat) const;
bool operator!=(const rational<T>& rat) const;

(though you should probably be omitting <T>)

Further, if you omit the trailing const, you would not be able to compare with a const object on the left

const rational<int> a;
const rational<int> b;
if (a == b) { // error if you don't have the trailing const on operator==

const parameters in a const member function

void someFunction(const string& A) const

The last const means that the method will not change the state of the object referenced by *this inside it. The first const is saying that the function will not change the state of the argument - and it doesn't have any correlation with the second const, so you may have this:

void someFunction(string& A) const

In this case function may change state of A argument, but it may not change the state of its object.

For example (and this is a highly hypothetical example):

class MyIntArray
{
// some magic here in order to implement this array

public:
void copy_to_vector(std::vector<int> &v) const
{
// copy data from this object into the vector.
// this will modify the vector, but not the
// current object.
}

}

And this is the example where these two are combined:

class MyOutput
{
char prefix;
// This class contains some char which
// will be used as prefix to all vectors passed to it
public:
MyOutput(char c):prefix(c){}
void output_to_cout(const std::vector<int> &i) const
{
// iterate trough vector (using const_iterator) and output each element
// prefixed with c - this will not change nor the vector
// nor the object.
}

}

Oh, and take a look into this question: Use of 'const' for function parameters

Use of 'const' for function parameters in forward declarations

No, there is no good reason to do that*.

I'd argue that adding const in the declaration is a bad idea anyway, even if the definition has it too. Why? Because the language ignores it in the declaration, so there is no guarantee that the definition will have it. In any case, it is an implementation detail (essentially, choosing to use a const local variable), so it should appear, if needed, in the definition only.


* "that" refers to top-level const. Pointer to const or reference to const is not ignored by the language.

Why is const unnecessary in function declarations in header files for parameters passed by value?

The statement you quote is a bit misleading, because in C, all arguments are passed by value.* I suppose it is trying to distinguish between the arguments themselves and, for the special case of arguments that are pointers, their referents.

In any event, the point is that const-qualifying a function parameter in the function declaration conveys no information whatever to callers. Regardless of such qualification, the function cannot modify the caller's copy of any argument anyway, because arguments are passed by value.

*Note, however, that arrays are never passed at all. In function call expressions, as in most contexts, array values "decay" to pointers, and those pointers are passed by value. This produces an effect similar, but not identical, to what you would have if arrays were passed by reference.

When could it be inefficient to use `const` for function arguments?

My question is about when using const could be not advisable.

Not using const in a function definition, to improve efficiency, is not a real concern in C.

C - Which is the advantage for the user of const in parameters of functions that are not pointers?

Some people here say that you should write const if you won't change the variable - as you can declare any local variable const. (The main goal is to keep you from accidently changing the variable, but also may help the compiler to optimize your code)

Other people say that you shouldn't declare function parameter const, because it will expose some of the implementation in the API.

I think that they both are right! That is why you can omit the const in function declaration: You can write void func(int); in a header, but implement it void func(const int i) {} in the code file.



Related Topics



Leave a reply



Submit