• RVO vs. TCO

    From Marcel Mueller@3:633/280.2 to All on Tue Jun 10 01:16:12 2025
    Hello,

    does a tail call prevent return value optimization?


    vector<XXX> func1();

    vector<XXX> func2()
    {
    if (some_condition)
    return func1();

    vector<XXX> result;
    // populate result

    return result;
    }

    AFAIK RVO requires all paths of a function to return the same instance.
    But can this also work with tail calls like func1()?

    If not, the following work around could be preferred:

    vector<XXX> func3()
    {
    vector<XXX> result;

    if (some_condition)
    result = func1(); // forces move
    else
    ; // populate result

    return result;
    }


    Marcel

    --- MBSE BBS v1.1.1 (Linux-x86_64)
    * Origin: MB-NET.NET for Open-News-Network e.V. (3:633/280.2@fidonet)