Is there any reason to use the 'auto' keyword in C++03?

后端 未结 10 1763
[愿得一人]
[愿得一人] 2020-11-28 02:27

Note this question was originally posted in 2009, before C++11 was ratified and before the meaning of the auto keyword was drast

10条回答
  •  孤独总比滥情好
    2020-11-28 03:11

    I use this keyword to explicitly document when it is critical for function, that the variable be placed on the stack, for stack-based processors. This function can be required when modifying the stack prior to returning from a function (or interrupt service routine). In this case I declare:

    auto unsigned int auiStack[1];   //variable must be on stack
    

    And then I access outside the variable:

    #define OFFSET_TO_RETURN_ADDRESS 8     //depends on compiler operation and current automatics
    auiStack[OFFSET_TO_RETURN_ADDRESS] = alternate_return_address;
    

    So the auto keyword helps document the intent.

提交回复
热议问题