99

I think the question is clear enough. Will the auto keyword auto-detect const-ness, or always return a non-const type, even if there are eg. two versions of a function (one that returns const and the other that doesn't).

Just for the record, I do use const auto end = some_container.end() before my for-loops, but I don't know if this is necessary or even different from normal auto.

4 Answers 4

112
const auto x = expr;

differs from

auto x = expr;

as

const X x = expr;

differs from

X x = expr;

So use const auto and const auto& a lot, just like you would if you didn't have auto.

Overload resolution is not affected by return type: const or no const on the lvalue x does not affect what functions are called in expr.

1
  • 18
    No. If you're not going to (or supposed to) modify the variable, it should be declared const. Feb 20, 2016 at 15:38
37

Maybe you are confusing const_iterator and const iterator. The first one iterates over const elements, the second one cannot iterate at all because you cannot use operators ++ and -- on it.

Note that you very seldom iterate from the container.end(). Usually you will use:

const auto end = container.end();
for (auto i = container.begin(); i != end; ++i) { ... }
8
  • 12
    cbegin and cend return a const_iterator by value. const auto still has it purpose and is not redundant.
    – dalle
    Feb 21, 2011 at 20:03
  • 2
    So const auto is useful in this general case?
    – rubenvb
    Feb 21, 2011 at 20:04
  • @dalle: I had removed my paragraph before you could comment, having realized that I had said just nonsensical things :)
    – Benoit
    Feb 21, 2011 at 20:08
  • 2
    Someone's been writing too much sql. :) Feb 23, 2011 at 13:16
  • 3
    auto is modified by const as by &, it can be used in a foreach for example : en.cppreference.com/w/cpp/language/auto
    – Janosimas
    Jul 31, 2015 at 21:31
7

Consider you have two templates:

template<class U> void f1( U& u );       // 1
template<class U> void f2( const U& u ); // 2

auto will deduce type and the variable will have the same type as the parameter u (as in the // 1 case), const auto will make variable the same type as the parameter u has in the // 2 case. So const auto just force const qualifier.

3

Compiler deduces the type for the auto qualifier. If a deduced type is some_type, const auto will be converted to const some_type. However, a good compiler will examine the whole scope of auto variable and find if the value of it changes anywhere. If not, compiler itself will deduce type like this: auto -> const some_type. I've tried this in Visual studio express 2012 and machine code produced is the same in both cases, I'm not sure that each and every compiler will do that. But, it is a good practice to use const auto for three reasons:

  • Preventing coding errors. You intended for this variable not to change but somehow somewhere in its scope, it is changed.
  • Code readability is improved.
  • You help the compiler if for some reason it doesn't deduce const for auto.
3
  • The compiler will not deduce const if it is possible... It will only add const if the expression it is deducing the type of is already const by itself. If a compiler would just add const for the sake of it being possible, program semantics might be broken, such as const vs non-const member functions being called depending on if the particular compiler can deduce the const or not. So I think your last point is wrong.
    – rubenvb
    Dec 30, 2016 at 10:00
  • @rubenvb The C++ standards allow compilers to reorganize code in whichever way it suits them, depending on desired optimizations. It is perfectly legal for a compiler to deduce const for auto if it is not changing program functionality in any way. That also includes checks if there are const and non-const member functions available. Compiler will do it right.
    – BJovke
    Dec 30, 2016 at 14:38
  • A good compiler will examine the whole scope of auto variable and find if the value of it changes anywhere. If not, compiler itself will deduce type like this: auto -> const some_type. If you don't try to change the variable, how do you know if it's been deduced as const or not? Anyway, I don't believe this. Jul 3, 2021 at 23:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.