1

When are auto and decltype (for lack of a better word) "evaluated"? At runtime, or when the code is compiled?

If it is the former, does using them have any noticeable performance penalty compared to hard-coding the variable type?

IQAndreas
  • 8,060
  • 8
  • 39
  • 74

1 Answers1

2

Obviously at compile type; you can not have a variable with an unknown type at runtime. So using auto should not incur any runtime performance penalty compared to hand-coded the corresponding type. (I mean exactly the same type as auto would use. If you hand-code a different type, your performance obviously might change in whatever direction.)

Petr
  • 9,812
  • 1
  • 28
  • 52
  • 1
    I think it is Scott Meyers who pointed out that you might even sometimes get slightly *better* performance, because auto uses the exact type and avoids any unintended type conversions. – Bo Persson Oct 29 '15 at 10:42
  • @BoPersson, that's why I said _corresponding_ type (meaning the exact type that `auto` would use). If you hand-code a different type, you can obviously have _better_ performance in some cases, and you can obviously have _worse_ performance in some cases. – Petr Oct 29 '15 at 10:45
  • 2
    @BoPersson: I disagree with Scott there, who seems to be trying to cargo cult everybody to the religion of `auto`. It's not switching to `auto` that gives you better performance; it's removing the redundant/broken/extraneous type conversion from your program's semantics, whether you achieve that by writing `auto` or by simply fixing your original typename. It seems dishonest to laud `auto` with performance-enhancing skills based on that. – Lightness Races in Orbit Oct 29 '15 at 11:02