c++c++11autotrailing-return-type

Can i use auto or decltype instead trailing return type?


I find trailing return type so easy to define the return of a function that returns a complicated types e.g:

auto get_diag(int(&ar)[3][3])->int(&)[3]{ // using trailing return type
    static int diag[3]{
        ar[0][0], ar[1][1], ar[2][2]
    };
    return diag;
}

auto& get_diag2(int(&ar)[3][3]){ // adding & auto because otherwise it converts the array to pointer
    static int diag[3]{
        ar[0][0], ar[1][1], ar[2][2]
    };
    return diag;
}

int main(){

    int a[][3]{
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };

    decltype(get_diag(a)) diag{
        get_diag(a)
    };

    for (auto i : diag)
        std::cout << i << ", ";
    std::cout << std::endl;

    decltype(get_diag2(a)) diag2{
        get_diag2(a)
    };

    for (auto i : diag2)
        std::cout << i << ", ";
    std::cout << std::endl;


    std::cout << std::endl;
}

Solution

  • auto& get_diag2(int(&ar)[3][3]){ // adding & auto because otherwise it converts the array to pointer
        static int diag[3]{
            ar[0][0], ar[1][1], ar[2][2]
        };
        return diag;
    }
    

    Will not work in a C++11 compiler. Using auto without a trailing return type was added to C++14 and acts like how auto works when using it for a variable. This means it will never return a reference type so you have to use auto& to return a reference to the thing you want to return.

    If you do not know if you should return a reference or a value (this happens a lot in generic programming) then you can use decltyp(auto) as the return type. For example

    template<class F, class... Args>
    decltype(auto) Example(F func, Args&&... args) 
    { 
        return func(std::forward<Args>(args)...); 
    }
    

    will return by value if func returns by value and return by reference if func returns a reference.


    In short if you are using C++11 you have to specify the return type, either in front or as a trailing return type. In C++14 and above you can just use auto/decltype(auto) and let the compiler deal with it for you.