Home  >  Q&A  >  body text

c++默认参数顺序的问题

c++默认参数顺序为什么是自右往左?为什么不能从左往右呢?请问这是什么原因。

PHPzPHPz2764 days ago546

reply all(3)I'll reply

  • 高洛峰

    高洛峰2017-04-17 15:00:48

    Can support any number of parameters from right to left

    Parameters need to be pushed onto the stack. The stack is first in, last out. If it is pushed into the stack from left to right, it will be very troublesome to find the first leftmost parameter~

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 15:00:48

    The stacking order is the same as big-endian and little-endian, either one is acceptable. The default choice of C language is from right to left. It should be because the stack grows downward, so the memory arrangement of pushed parameters will be exactly the same as the order of writing. It may be more convenient to debug and look at the memory. However, actual code access is in the form of stack pointer + offset, so it has no special meaning for the machine.
    Whether it can support variable parameters mainly depends on whether the caller is responsible for clearing the stack. For example, __stdcall also pushes the stack from right to left, but the callee clears the stack, so variable parameters are not supported.
    __pascal imitates the PASCAL language and pushes the stack from left to right, which is no longer supported by VC.

    reply
    0
  • 迷茫

    迷茫2017-04-17 15:00:48

    Assume from left to right

    int f(int a = 1, int b);

    Then did the 1 of your f(1) pass to a or b? . So it cannot go from left to right. .

    reply
    0
  • Cancelreply