Why do I need to explicitly write the 'auto' keyword?

前端 未结 7 722
谎友^
谎友^ 2020-12-14 05:34

I am moving towards C++11 from C++98 and have become familiar with the auto keyword. I was wondering why we need to explicitly declare auto if the

7条回答
  •  一个人的身影
    2020-12-14 05:54

    Adding to previous answers, one extra note from an old fart: It looks like you may see it as an advantage to be able to just start using a new variable without in any way declaring it.

    In languages with the possibility of implicit definition of variables this can be a big problem, especially in larger systems. You make one typo and you debug for hours only to find out you unintentionally introduced a variable with a value of zero (or worse) - blue vs bleu, label vs lable ... the result is you can't really trust any code without thorough checking on precise variable names.

    Just using auto tells both compiler and maintainer that it is your intention to declare a new variable.

    Think about it, to be able to avoid this sort of nightmares the 'implicit none' statement was introduced in FORTRAN - and you see it used in all serious FORTRAN programs nowadays. Not having it is simply ... scary.

提交回复
热议问题