0

Recently i was looking for a solution to sort only the values of map and then i found this code

    template <typename T1, typename T2>
    struct less_second {
    typedef pair<T1, T2> type;
    bool operator ()(type const& a, type const& b) const {
        return a.second < b.second;
    }
};

map<string, int> mymap;
// …

vector<pair<string, int> > mapcopy(mymap.begin(), mymap.end());
sort(mapcopy.begin(), mapcopy.end(), less_second<string, int>());

The first thing that stuck my mind was why in many sample code and solutions I find people frequently using the const keyword is it a good practice to do that, if so why?

t.niese
  • 39,256
  • 9
  • 74
  • 101
user173379
  • 21
  • 6
  • If you have two completely different questions then create two, and doen't ask them in one. Your second one is answered here: [What are C++ functors and their uses?](https://stackoverflow.com/questions/356950) – t.niese Dec 26 '19 at 11:23
  • Sorry, Thanks for the link i have edited the question now and omitted the second part – user173379 Dec 26 '19 at 11:25
  • Then the first part is a duplicate of this question: https://stackoverflow.com/questions/117293/use-of-const-for-function-parameters – Gomino Dec 26 '19 at 11:34
  • https://isocpp.org/wiki/faq/const-correctness – asynts Dec 26 '19 at 11:35
  • 1
    If the intent is that something will not be changed, then specifying it as `const` is useful and considered good practice - it allows the compiler to detect if a (possibly unintended) attempt is made to change it. The alternative, not marking it `const`, means that the compiler will not report attempts to change it - which can give unpleasant surprises if some other code ASSUMES it has not changed. – Peter Dec 26 '19 at 11:40

2 Answers2

1

Why use const?

My take is that you should try to use const as much as you can. Mutable values are always a source of errors. You can avoid these errors by avoiding mutable values.

Function of const in C++

const is used a lot because it has a lot off different meanings:

If we look at this signature, we can see two types of const bool operator ()(type const& a, type const& b) const {...}: There is a const reference const &a and const &b, which just means that you cannot modify a or b, which you passed as reference.

There is also the const at the end of the method, which means that this method does not modify the instance. Figure a get-method. This method will not modify the instance it gets the value from. You can signal this to the caller by using T get_element(int at_index) const {...}.

There are further const pointers.

Here you could change the value in the address, but not the address itself:

int *const a = new int;
*a = 1; // legal
a = &var; // illegal

Here you can change the address the pointer is pointing to, but not the value in that address.

const int *a = new int;
*a = 1; // illegal
a = &var; // legal

(Hope I did not confuse the two)

Have a pointer that does not allow any change:

const int * const a = new int;
*a = 1; // illegal
a = &var; // illegal

Further reading:

When to use constant pointers instead of const references.

YT - Const in C++

There is furthermore constexpr, which can calculate a value at compile time. This is different than the const keyword, and might confuse you at the beginning.

User12547645
  • 6,955
  • 3
  • 38
  • 69
1

The const in the parameters of this function allows the function to be called with const values (in the case of int a value like 1 is const). And also since the parameters are a reference, the const guarantees that the original variables will not be changed.

The const after the member function declaration, guarentees that an object of that class is not modified when that function is called.

So by looking at the definition of the function, we can automatically know the guarentees and what type of variables we can use.

Also knowing these assumptions, the compiler can create better code.

Robert Andrzejuk
  • 5,076
  • 2
  • 22
  • 31