如何解决Lambda捕获,[] vs [&]并调用类对象的成员函数
我有一个class.h
文件:
class foo {
public:
int lengthyfunction(int& x,int y){
int z = 0;
for(count = 1; count <= 1000000000; count++)
z = x + 1;
printf("Address of x in function is %p\n",&x);
printf("Address of y in function is %p\n",&y);
printf("Address of count in function is %p\n",&count);
printf("Address of object in function is %p\n",this);
return x / y;
}
private:
int count;
};
和一个main.cpp
文件:
#include <iostream>
#include <chrono>
#include <functional>
#include "class.h"
auto measure = [&](double& time,auto&& function,auto&&... parameters) -> decltype(auto){
//A lambda that accepts an arbitrary function and records its computational time
const std::chrono::steady_clock::time_point startTimePoint = std::chrono::steady_clock::now();
decltype(auto) returnValue = std::forward<decltype(function)>(function)(std::forward<decltype(parameters)>(parameters)...);
const std::chrono::steady_clock::time_point stopTimePoint = std::chrono::steady_clock::now();
const std::chrono::duration<double> timeSpan = std::chrono::duration_cast<std::chrono::duration<double>>(stopTimePoint - startTimePoint);
std::cout << "Computation took " << timeSpan.count() << " seconds." << std::endl;
time = timeSpan.count();
printf("Address of time in lambda is %p\n",&time);
return returnValue;
};
int main() {
foo f;
using namespace std::placeholders;
auto function = std::bind(&foo::lengthyfunction,f,_1,_2);
double ftime;
int x = 20;
int y = 10;
printf("Address of x in main is %p\n",&x);
printf("Address of y in main is %p\n",&y);
printf("Address of object in main is %p\n",&f);
printf("Address of time in main is %p\n",&ftime);
printf("---\n");
decltype(auto) result = measure(ftime,function,x,y);
getchar();
}
具体来说,请注意,根据参考,auto measure = [&](double& time,auto&&... parameters) -> decltype(auto)
中的main.cpp
是[&]
。
在我的Windows计算机Visual Studio Release版本上,此输出为:
Address of x in main is 00000022DD8FFC44
Address of y in main is 00000022DD8FFC40
Address of object in main is 00000022DD8FFC48
Address of time in main is 00000022DD8FFC50
---
Address of x in function is 00000022DD8FFC44
Address of y in function is 00000022DD8FFC00
Address of count in function is 00000022DD8FFC64
Address of object in function is 00000022DD8FFC64
Computation took 3.01977 seconds.
Address of time in lambda is 00000022DD8FFC50
我可以理解,由于lengthyfunction
通过引用接受x
,因此x
的地址在main和函数中是相同的。 time
的类似推理。同样,我知道y
的地址在main和函数之间是不同,因为即使捕获是[&]
也没有通过引用传递它。到目前为止还好。
(Q1)然而,令人惊讶的是,为什么foo
对象的地址在main和函数之间是不同的?不是通过对main中定义的同一对象的measure
调用来调用该函数吗?
删除捕获中的&
并进行以下操作:
auto measure = [](double& time,auto&&... parameters) -> decltype(auto)
赋予不同的地址但具有相同的基础结果(x
和time
地址在main和function中相同,y
和foo
对象地址不同。)有关完成的信息,请参见此处是这种情况下的结果:
Address of x in main is 0000001892CFFDD4
Address of y in main is 0000001892CFFDD0
Address of object in main is 0000001892CFFDD8
Address of time in main is 0000001892CFFDE0
---
Address of x in function is 0000001892CFFDD4
Address of y in function is 0000001892CFFD90
Address of count in function is 0000001892CFFDF4
Address of object in function is 0000001892CFFDF4
Computation took 3.03224 seconds.
Address of time in lambda is 0000001892CFFDE0
(Q2)在这种情况下,是否有任何理由比[]
更喜欢[&]
?在大多数情况下,我更喜欢使用[&]
以避免产生不必要和意外的对象副本。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。