Modern C++——函数参数类型的分类和使用

在C++中,函数参数主要有两种方式:值类型和引用类型。其中引用类型又分为:左值引用和C++11引入的右值引用。下面我们分别对其进行介绍。

基本定义

要弄清楚这些概念,我们先要知道什么是左值,什么是右值。

  • 左值是指在内存中有确定存储地址的对象,可以在表达式中出现在赋值操作符的左侧。
  • 右值是指在表达式中没有持久存储地址的临时对象,通常出现在赋值操作符的右侧。

比如下面的代码中,a是左值,10是右值。

int a = 10;

值类型

值类型是最基本的参数类型。

在函数被调用时:

  • 如果实参是左值,它就会被复制一份到形参中(调用复制构造函数)。这意味着在函数体内对形参的任何修改都不会影响到实参本身。值传递适用于基本数据类型(如int, float等)和对象的小型拷贝开销可以接受的情况。
  • 如果实参是右值引用,则右值引用的对象会触发形参的移动构造函数。这减少了不必要的资源分配和释放,提高了性能。

我们设计了一个Custom类,它会在各个构造函数中打印this指针的值和相关标记信息,用于辅助我们查看和分析后续研究代码。

class Custom {
public:
    Custom(int value) : value(value) {
        std::cout << "Constructor Custom(" << value << "). this = " << this << std::endl;
    }
    
    Custom(const Custom& custom) : value(custom.value) {
        std::cout << "Copy constructor Custom(" << custom.value << "). this = " << this << " from " << &custom << std::endl;
    }

    Custom& operator=(const Custom& custom) {
        value = custom.value;
        std::cout << "Copy assignment operator Custom(" << custom.value << "). this = " << this << " from " << &custom << std::endl;
        return *this;
    }

    Custom(Custom&& custom) : value(custom.value) {
        std::cout << "Move constructor Custom(" << custom.value << "). this = " << this << " from " << &custom << std::endl;
        custom.value = 0;
    }

    Custom& operator=(Custom&& custom) {
        value = custom.value;
        std::cout << "Move assignment operator Custom(" << custom.value << "). this = " << this << " from " << &custom << std::endl;
        return *this;
    }

    ~Custom() {
        std::cout << "Destructor Custom(" << value << "). this = " << this << std::endl;
    }

    friend std::ostream& operator<<(std::ostream& os, const Custom& custom) {
        os << custom.value;
        return os;
    }

public:
    int get_value() const {
        return value;
    }

    void set_value(int value) {
        this->value = value;
    }

private:
    int value;
};

下面的函数func的参数是值类型。

void func(Custom custom) {
    std::cout << "func(Custom custom) custom = " << custom << " from " << &custom << std::endl;
}

然后分别给func传入左值和右值,观察其执行过程。

int main(int argc, char* argv[]) {
    Custom custom(1);
    std::cout << "build custom in main" << std::endl;
    
    std::cout << "call func(custom)" << std::endl;
    func(custom);

    std::cout << "call func(std::move(custom))" << std::endl;
    func(std::move(custom));

    std::cout << "main end" << std::endl;
    return 0;
}

可以看到给值类型传入左值,会触发拷贝构造函数(过程2);给值类型传入右值引用,会触发移动构造函数(过程3)。
在这里插入图片描述

左值引用

左值引用(Lvalue Reference)是C++中的一种引用类型,用于引用一个左值(Lvalue)。左值引用的语法是使用单个 & 符号。

非常量左值引用

如果函数的参数是左值引用,这意味着函数直接操作的是原变量的内存地址,因此对形参的修改会反映到实参上。使用左值引用可以避免大型对象或结构体的复制开销,提高效率。

void func(Custom& custom) {
    std::cout << "func(Custom custom) custom = " << custom << " from " << &custom << std::endl;
}

int main(int argc, char* argv[]) {
    Custom custom(1);
    Custom& custom2 = custom;
    const Custom& custom3 = custom;
    func(custom);
    func(custom2);
    // func(custom3);  // error
    // func(std::move(custom)); // error: cannot bind non-const lvalue reference of type 'Custom&' to an rvalue of type 'Custom'

    std::cout << "main custom = " << custom << " from " << &custom << std::endl;
    return 0;
}

需要注意的是:非常量左值引用(T&)只能绑定到左值,不能绑定到右值。

常量左值引用

有些时候,我们仅仅希望避免复制操作,而不希望函数修改原变量的值,这样我们就需要使用常量左值引用。

void func_const(const Custom& custom) {
    std::cout << "func_const(const Custom& custom) custom = " << custom << " from " << &custom << std::endl;
}

int main(int argc, char* argv[]) {
    Custom custom(1);
    Custom& custom2 = custom;
    const Custom& custom3 = custom;

    func_const(custom);
    func_const(custom2);
    func_const(custom3);
    func_const(std::move(custom));  // const lvalue reference can bind to rvalue

    std::cout << "main custom = " << custom << " from " << &custom << std::endl;
    return 0;
}

我们给常量左值引用形参传递值、左值引用、常量左值引用、右值引用都是可以的,其执行效果是一样的。

在这里插入图片描述

需要注意的是,传递右值引用并不出触发Custom的移动构造函数执行。我们不要被std::move这个函数名给骗了,它不会进行任何的“移动”操作。它只是将一个左值/右值强制转换成右值。

    constexpr typename std::remove_reference<_Tp>::type&&
    move(_Tp&& __t) noexcept
    { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }

右值引用

右值引用(Rvalue Reference)是C++11引入的一种引用类型,用于引用一个右值(Rvalue)。右值引用的语法是使用双 && 符号。

右值引用通常与移动语义结合使用,用于优化资源的转移,如动态分配的内存、文件句柄等。通过右值引用传递,我们可以避免在函数调用中不必要的资源复制,而是直接“移动”资源给函数内的变量。

void func(Custom&& custom) {
    std::cout << "func(Custom custom) custom = " << custom << " from " << &custom << std::endl;
}

void func_const(const Custom&& custom) {
    std::cout << "func(Custom custom) custom = " << custom << " from " << &custom << std::endl;
}

int main(int argc, char* argv[]) {
    Custom custom(1);
    Custom& custom2 = custom;
    const Custom& custom3 = custom;
    // func(custom); // error: cannot bind rvalue reference of type 'Custom&&' to lvalue of type 'Custom'
    // func(custom2); // error
    // func(custom3); // error
    func(std::move(custom));

    // func_const(custom); // error: cannot bind rvalue reference of type 'const Custom&&' to lvalue of type 'Custom'
    // func_const(custom2); // error
    // func_const(custom3); // error
    func_const(std::move(custom));
    std::cout << "main end" << std::endl;
    return 0;
}

右值引用形参只能接受右值引用实参。

总结

  • 当右值引用需要做复制操作时,会触发移动构造函数。
  • 值类型形参会触发复制操作,而引用类型不会触发复制操作。所以如果希望触发移动构造函数,需要将参数形参设计为值类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

breaksoftware

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值