C++ templates: (4)、引用折叠

引用折叠(Reference collapsing)是C++11中引入的一种规则,用于处理函数模板参数的类型和值完全匹配的情况。当一个非const左值引用和一个右值引用绑定到同一块内存时,引用折叠会发生。

引用折叠的规则如下:

  1. 如果两个引用类型都是左值引用,那么它们会折叠为左值引用。
  2. 如果其中一个引用类型是左值引用,另一个是右值引用,那么它们会折叠为左值引用。
  3. 如果两个引用类型都是右值引用,那么它们会折叠为右值引用。

在函数模板参数中,T&&称作万能引用,可以转发const 左值引用,左值引用和右值引用。

#include <iostream>
using namespace std;
#include <type_traits>

template<typename T>
void test(T&& a){
    if(std::is_lvalue_reference_v<T>)
            cout << "左值引用" << endl;
    else if(std::is_rvalue_reference_v<T>)
        cout << "右值引用" << endl;
    else
        cout << "不是引用" << endl;

    if(std::is_const_v<std::remove_reference_t<T>>)
        cout << "常量" << endl;

    cout << "-------------" << endl;
}

template<typename T>
void test2( T a){
    if(std::is_lvalue_reference_v<decltype(a)>)
        cout << "左值引用" << endl;
    else if(std::is_rvalue_reference_v<decltype(a)>)
        cout << "右值引用" << endl;
    else
        cout << "不是引用" << endl;

    if(std::is_const_v<remove_reference_t<decltype(a)>>)
        cout << "常量" << endl;
    cout << "-------------" << endl;
}

template<typename T>
void test3( T& a){
    if(std::is_lvalue_reference_v<decltype(a)>)
        cout << "左值引用" << endl;
    else if(std::is_rvalue_reference_v<decltype(a)>)
        cout << "右值引用" << endl;
    else
        cout << "不是引用" << endl;

    if(std::is_const_v<remove_reference_t<decltype(a)>>)
        cout << "常量" << endl;

    cout << "-------------" << endl;
}

class A{
public:
    int data = 10;
};

int main(){
    A a;
    const A c = {};
    test(a);
    test(A{});
    test(c);
    cout << endl;

    test2(a);
    test2(A{});
    test2(c);
    cout << endl;

    test3(a);
    //test3(A{}); ///
    test3(c);
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值