C++的&和&&

转载请注明文章出处:https://ssrvps.org/archives/3875

c++的&和&&是两个常用且容易让人混淆迷惑的运算符,其中&符号有三种用途,&&有两种。本文对这两个运算符的用途做简要总结。

&的用途

第一种用途:位运算中的“与”(AND)。位运算十分高效,数据分片时常会用到,例如网络数据报头、IP地址段、UTF-8编码等。

第二种用途:取地址。这个功能在C中比较常见,比如取函数地址、变量地址。用法示例:

int b = 10;
int *a = &b;  // a指针指向b的存储地址

// 声明函数
int add(int a, int b) {return a + b}
// 声明函数指针类型
int (*functionPtr)(int, int);
// 声明和初始化指针, &取函数的地址
functionPtr addPtr = &add;

第三种用途:引用。这个功能是C++的补充,常用在函数传参(C中一般用指针)、临时变量引用等。用法示例:

// 声明v1和v2两个参数引用传递
template <typename T>
T product(const std::vector<T>& v1, const std::vector<T>& v2) {...}

std::vector<std::vector<int> > vecs(10);
// 引用取出数组里的值,避免复制
auto& vec = vecs[0];

可以看到,三种用法都很基础,使用上很简单。

&&的用途

第一种用途:“与”(AND)逻辑运算符。做条件判断时,&&常用来连接多个条件。

第二种用途:右值引用,这个功能自C++11起才可用。移动语义是C++11新增的重要功能,其重点是对右值的操作。右值可以看作程序运行中的临时结果,右值引用可以避免复制提高效率。&&用法示例:

#include <iostream>
struct Foo {
  ~Foo() {std::cout << "destruction" << std::endl;}
};

Foo FooFactory() {
  return Foo();
}

int main() {
  std::cout << "before copy constructor..." << std::endl;
  Foo foo1 = FooFactory();
  std::cout << "after copy constructor..." << std::endl << std::endl;
  // 引用右值,避免生成新对象
  Foo&& foo2 = FooFactory();
  std::cout << "life time ends!" << std::endl << std::endl;

  return 0;
}

clang编译器(加-fno-elide-constructors选项)编译上述代码, 运行结果如下:

before copy constructor...
destruction
destruction
after copy constructor...

destruction
life time ends!

destruction
destruction

从输出结果看,第二种写法少了一次destruction输出。这意味着通过右值引用(&&),foo2直接引用FooFactory返回的对象,避免了对象复制。

注意: 由于“复制省略(Copy elision)”优化技术,一些编译器输出可能与上述不同。例如g++编译器会略过临时对象复制以优化性能。g++ 4.8.5编译程序输出结果如下:

before copy constructor...
after copy constructor...

life time ends!

destruction
destruction

clang编译的结果对比,省去了三次临时对象的生成、析构。

参考

  1. https://stackoverflow.com/questions/13099603/c11-move-constructor-not-called-default-constructor-preferred
留言评论

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

Captcha Code

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select * from vps_autoblog_queue where is_running >0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select count(*) from vps_autoblog_queue where task_id=6 and is_running>0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,fetched,is_running,last_date_time from vps_autoblog_queue where task_id=6 and source_url_key='85f8e27ce55bd78fd33f47d2b1409198'

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
insert into vps_autoblog_queue(task_id,source_url,source_url_key,create_date_time,not_check_stoped,post_interval) values(6,'https://www.hijk.pw/post-sitemap.xml','85f8e27ce55bd78fd33f47d2b1409198',1750611100,0,0)

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select count(*) from vps_autoblog_queue where task_id=4 and is_running>0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,fetched,is_running,last_date_time from vps_autoblog_queue where task_id=4 and source_url_key='54b1dccf98c13c81f546faf82132bc77'

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
insert into vps_autoblog_queue(task_id,source_url,source_url_key,create_date_time,not_check_stoped,post_interval) values(4,'https://ssr.tools/sitemap-posttype-post.2020.xml','54b1dccf98c13c81f546faf82132bc77',1750611100,0,0)

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select count(*) from vps_autoblog_queue where task_id=2 and is_running>0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,fetched,is_running,last_date_time from vps_autoblog_queue where task_id=2 and source_url_key='91372ddcd9e4809b115feaa5b6f00815'

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
insert into vps_autoblog_queue(task_id,source_url,source_url_key,create_date_time,not_check_stoped,post_interval) values(2,'https://tlanyan.me/post-sitemap.xml','91372ddcd9e4809b115feaa5b6f00815',1750611100,0,0)

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select count(*) from vps_autoblog_queue where task_id=7 and is_running>0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,fetched,is_running,last_date_time from vps_autoblog_queue where task_id=7 and source_url_key='569e17863f7693f1afa9e9256af9dcff'

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
insert into vps_autoblog_queue(task_id,source_url,source_url_key,create_date_time,not_check_stoped,post_interval) values(7,'https://aisoa.cn/sitemap.xml','569e17863f7693f1afa9e9256af9dcff',1750611100,0,0)

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select count(*) from vps_autoblog_queue where task_id=9 and is_running>0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,fetched,is_running,last_date_time from vps_autoblog_queue where task_id=9 and source_url_key='cc9416f8df40d8c3a48e66856b1cd6fd'

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
insert into vps_autoblog_queue(task_id,source_url,source_url_key,create_date_time,not_check_stoped,post_interval) values(9,'https://www.v2rayssr.com/sitemap.xml','cc9416f8df40d8c3a48e66856b1cd6fd',1750611100,0,0)

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select count(*) from vps_autoblog_queue where task_id=10 and is_running>0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,fetched,is_running,last_date_time from vps_autoblog_queue where task_id=10 and source_url_key='74714f67855820f0c701ce905b231f27'

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
insert into vps_autoblog_queue(task_id,source_url,source_url_key,create_date_time,not_check_stoped,post_interval) values(10,'https://doubibackup.com/sitemap.xml','74714f67855820f0c701ce905b231f27',1750611100,0,0)

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,is_running,last_post_time,post_interval,not_check_stoped from vps_autoblog_queue WHERE is_running>0 and fetched=0 order BY id ASC

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,sn_id from vps_autoblog_queue WHERE is_running=0 and fetched=0 order BY id ASC LIMIT 1