0

I'm asking for a discussion of the circumstances in which, in C++, declaring the parameter of a function as const will actually improve the generated code (rather than improving the source code, where appropriate const unambiguously adds value).

The initial motivation was code like the following lacking the const:

#include <vector>
class foo {
    public:
        foo(const std::vector<int> bar); // construct foo from vector of int
    /*...*/
    };
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
fgrieu
  • 2,724
  • 1
  • 23
  • 53
  • Please define "improve" (e.g., performance, readability)? – 101010 Jun 23 '15 at 11:53
  • @CoryKramer: yes, I made a duplicate. Thanks for the pointer. I thus voted to close my own question (it works..)! Will delete it, I guess. – fgrieu Jun 23 '15 at 11:55
  • No worries, it was a great question (which is why the other has so many upvotes), I just think the duplicate has some great answers already. – Cory Kramer Jun 23 '15 at 11:55
  • This is a bad example, because what you have is just a function *declaration*, therefore the `const` gets ignored. So, no difference at all WRT not having a `const` in there. – juanchopanza Jun 23 '15 at 11:56

0 Answers0