c++optimizationerror-handlingnrvo

Out params vs. NRVO in the presence of error codes


We have a code base that uses out params extensively because every function can fail with some error enum. This is getting very messy and the code is sometimes unreadable.

I want to eliminate this pattern and bring a more modern approach.

The goal is to transform:

error_t fn(param_t *out) {  
    //filling 'out'
}
param_t param;
error_t err = fn(&param);

into something like:

std::expected<error_t, param_t> fn() {
    param_t ret;
    //filling 'ret'
    return ret;
}
auto& [err, param] = fn();

The following questions are in order to convince myself and others this change is for the best:

  1. I know that on the standard level, NRVO is not mandatory (unlike RVO in c++17) but practically is there any chance it won't happen in any of the major compilers?
  2. Are there any advantages of using out parameters instead of NRVO?
  3. Assuming NRVO happens, is there a a significant change in the generated assembly (assuming an optimized expected implementation [perhaps with the boolean representing whether an error occured completly disappear])?

Solution

  • First off, a few assumptions:

    1. We are looking at functions that are not being inlined. It's going to be almost guaranteed to be absolutely equivalent in that case.

    2. We are going to assume that the call sites of the function actually check the error condition before using the returned value.

    3. We are going to assume that the returned value has not been pre-initialized with partial data.

    4. We are going to assume that we only care about optimized code here.

    That being established:

    I know that on the standard level, NRVO is not mandatory (unlike RVO in c++17) but practically is there any chance it won't happen in any of the major compilers?

    Assuming that NRVO is being performed is a safe bet at this point. I'm sure someone could come up with a contrived situation where it wouldn't happen, but I generally feel confident that in almost all use-cases, NRVO is being performed on current modern compilers.

    That being said, I would never rely on this behavior for program correctness. I.E. I wouldn't make a weird copy-constructor with side-effects with the assumption that it doesn't get invoked due to NRVO.

    Are there any advantages of using out parameters instead of NRVO?

    In general no, but like all things in C++, there are edge-case scenarios where it could come up. Explicit memory layouts for maximizing cache coherency would be a good use-case for "returning by pointer".

    Assuming NRVO happens, is there a a significant change in the generated assembly (assuming an optimized expected implementation [perhaps with the boolean representing whether an error occured completly disappear])?

    That question doesn't make that much sense to me. expected<> behaves a lot more like a variant<> than a tuple<>, so the "boolean representing whether an error occured completly disappear" doesn't really make sense.

    That being said, I think we can use std::variant to estimate:

    https://godbolt.org/g/XpqLLG

    It's "different" but not necessarily better or worse in my opinion.