When should I use C++14 automatic return type deduction?

前端 未结 7 2143
南笙
南笙 2020-11-28 19:05

With GCC 4.8.0 released, we have a compiler that supports automatic return type deduction, part of C++14. With -std=c++1y, I can do this:

auto          


        
7条回答
  •  情深已故
    2020-11-28 19:31

    For scenario 3 I would turn the return type of function signature with the local variable to be returned around. It would make it clearer for client programmers hat the function returns. Like this:

    Scenario 3 To prevent redundancy:

    std::vector, int>> foo() {
        decltype(foo()) ret;
        return ret;
    }
    

    Yes, it has no auto keyword but the principal is the same to prevent redundancy and give programmers who dont have access to the source an easier time.

提交回复
热议问题