Returning multiple values from a C++ function

后端 未结 21 2537
别跟我提以往
别跟我提以往 2020-11-22 01:04

Is there a preferred way to return multiple values from a C++ function? For example, imagine a function that divides two integers and returns both the quotient and the rema

21条回答
  •  梦毁少年i
    2020-11-22 01:50

    There is precedent for returning structures in the C (and hence C++) standard with the div, ldiv (and, in C99, lldiv) functions from (or ).

    The 'mix of return value and return parameters' is usually the least clean.

    Having a function return a status and return data via return parameters is sensible in C; it is less obviously sensible in C++ where you could use exceptions to relay failure information instead.

    If there are more than two return values, then a structure-like mechanism is probably best.

提交回复
热议问题