lundi 4 novembre 2019

Why use C++'s auto type and then add the return type after ->?

I was just reading through Bartek's blog when I found this example:

auto to_int( char const * const text ) -> std::optional<int>
{
    char * pos = nullptr;
    const int value = std::strtol( text, &pos, 0 );

    return pos == text ? std::nullopt : std::optional<int>( value );
}

Why did they use the idiom auto to_int( char const * const text ) -> std::optional<int>?

Isn't this the same as the traditional std::optional<int> to_int( char const * const text )?

Is there a reason to prefer one or the other in certain situations? If so, what would these situations be?

Aucun commentaire:

Enregistrer un commentaire