What is the difference between auto and decltype(auto) when returning from a function?

前端 未结 2 1816
醉话见心
醉话见心 2020-12-12 19:24

I rarely see decltype(auto) but when I do it confuses me because it seems to do the same thing as auto when returning from a function.



        
2条回答
  •  一个人的身影
    2020-12-12 19:51

    auto follows the template argument deduction rules and is always an object type; decltype(auto) follows the decltype rules for deducing reference types based on value categories. So if we have

    int x;
    int && f();
    

    then

    expression    auto       decltype(auto)
    ----------------------------------------
    10            int        int
    x             int        int
    (x)           int        int &
    f()           int        int &&
    

提交回复
热议问题