What does T&& (double ampersand) mean in C++11?

后端 未结 4 828
离开以前
离开以前 2020-11-22 03:55

I\'ve been looking into some of the new features of C++11 and one I\'ve noticed is the double ampersand in declaring variables, like T&& var.

Fo

4条回答
  •  轮回少年
    2020-11-22 04:07

    The term for T&& when used with type deduction (such as for perfect forwarding) is known colloquially as a forwarding reference. The term "universal reference" was coined by Scott Meyers in this article, but was later changed.

    That is because it may be either r-value or l-value.

    Examples are:

    // template
    template foo(T&& t) { ... }
    
    // auto
    auto&& t = ...;
    
    // typedef
    typedef ... T;
    T&& t = ...;
    
    // decltype
    decltype(...)&& t = ...;
    

    More discussion can be found in the answer for: Syntax for universal references

提交回复
热议问题