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

前端 未结 7 634
谎友^
谎友^ 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 06:08

    auto is a keyword which you can use in places where you normally need to specify a type.

      int x = some_function();
    

    Can be made more generic by making the int type automatically deduced:

      auto x = some_function();
    

    So it's a conservative extension to the language; it fits into the existing syntax. Without it x = some_function() becomes an assignment statement, no longer a declaration.

    0 讨论(0)
提交回复
热议问题